【发布时间】:2018-04-19 16:37:14
【问题描述】:
我只想禁用我的玩家管理器中的脚本。但是因为我的敌人是通过Set Active 生成的,所以公共游戏对象无法正常工作。我试过让我的玩家经理也成为一个预制件,但没有解决方案。我可以通过代码行通过脚本中的特定游戏对象手动指定它吗?
尽管这看起来很小,但我确实做了研究,并且大多数都指向专门使用 public GameObject。除非我对 C# 的搜索不够好。
public float lookRadius = 40f;
//public Casting stop;
Transform target;
UnityEngine.AI.NavMeshAgent agent;
Rigidbody theRigidBody;
void Start(){
target = PlayerManager.instance.player.transform;
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
void Update(){
float distance = Vector3.Distance (target.position, transform.position);
if (distance <= lookRadius)
{
agent.SetDestination (target.position);
if (distance <= agent.stoppingDistance)
{
FaceTarget ();
}
if (distance < 10f) // or some distance
{
//gameObject.GetComponent<Casting>().enabled = false;
Debug.Log("nearby heyy");
}
}
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
}
// Use this for initialization
public void OnObjectSpawn () {
//myRender = GetComponent<Renderer> ();
theRigidBody = GetComponent<Rigidbody>();
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere (transform.position, lookRadius);
}
}
【问题讨论】:
标签: unity3d