C# 如何在实例化对象之间的定义间隔内移动并留下实例化对象的痕迹?
有许多与我类似的问题,但没有一个能解决我的问题,所以这里是:) 我需要在玩家身后的物体之间设置一定间隔的轨迹。想象一下,它就像一辆在后面埋设地雷或设置围栏的车辆。有一些条件:C# 如何在实例化对象之间的定义间隔内移动并留下实例化对象的痕迹?,c#,unity3d,scripting,C#,Unity3d,Scripting,有许多与我类似的问题,但没有一个能解决我的问题,所以这里是:) 我需要在玩家身后的物体之间设置一定间隔的轨迹。想象一下,它就像一辆在后面埋设地雷或设置围栏的车辆。有一些条件: 按按钮(pres按钮->开始放置对象,释放按钮->停止放置对象)可触发创建对象 实例化对象必须彼此保持一定距离 创建的“杆”保持在场景中,玩家可以转身向fance添加更多的“杆”——添加的杆也必须在与现有杆保持一定距离的位置创建 这是我目前的解决方案(我与CompareTag合作使用了OnTiggerStay方法):
- 按按钮(pres按钮->开始放置对象,释放按钮->停止放置对象)可触发创建对象
- 实例化对象必须彼此保持一定距离
- 创建的“杆”保持在场景中,玩家可以转身向fance添加更多的“杆”——添加的杆也必须在与现有杆保持一定距离的位置创建
public class PoleTrailInstantiation : MonoBehaviour
*//variables....*
public GameObject Pole; *// object that would be instantiated, contains a trigger set to certain radius that prevents object instantiation and is tagged "CreatedPole"*
public Transform polePosition;
public Transform CreatorObject; *//empty object inside of player where "poles" are instantiated, this is where script is attached*
public GameObject Player;
private bool insideTrigger = false; *//this will tell me if I left trigger and instantiation would be allowed*
// Update is called once per frame
void FixedUpdate() *// this works only in fixedUpdate*
{
if ((Input.GetKey(KeyCode.Space) && isInsideTrigger == false))
{
InstantiatePole(); *// function that creates objects - "poles"*
}
insideTrigger = false;
} //end of FixedUpdate
private void OnTriggerStay(Collider other)
{
if (other.gameObject.CompareTag("CreatedPole")) *//compares if trigger player stays in belongs to "createdPole" object)*
{
insideTrigger = true;
print("OnTriggerStay activated!");
}
}