2016-09-26 2 views
0

플레이어와 연결된 NPC가 있지만 플레이어와 일정 거리에있을 때 멈출 수 없습니다. 이 일을 위해 웹상에 아무것도 없기 때문에 이것은 밤새 나를 좌절하게했습니다.플레이어의 특정 거리에서 NPC가 멈추지 않습니다.

서로 충돌하여 충돌합니다. 당신은 그것을 멀리로의 경우에만 이동 코드를 적용 할 필요가

using UnityEngine; 
using System.Collections; 

public class AI : MonoBehaviour { 
public Transform target; 
public int moveSpeed = 5; 
public int rotationSpeed = 2; 
public Transform myTransform; 

void Awake() 
{ 
    myTransform = transform; 
} 

void Start() 
{ 
    target = GameObject.FindWithTag ("Player").transform; 
} 

void Update() 
{ 
    myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * moveSpeed * Time.deltaTime); 
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 

} 
} 
+0

대상 주변에 타원형 타원을 놓고 적이 트리거 충돌 장치를 통과하면 AI가 움직이지 않도록합니다. 'OnTriggerEnter()'를 사용하십시오. –

+0

답장을 보내 주셔서 감사합니다.하지만이 코드는 아래에 나와있는 코드뿐만 아니라 작동하지 않습니다.이 튜토리얼에 대해 알고 계십니까? Unity에 새로운! –

답변

2

:

다음은 NPC를 이동 내 코드입니다. 충분히 가까우면 이동 코드를 적용하지 마십시오. 예 : 업데이트는 다음과 같아야합니다.

Vector3 Distance = target.position - myTransform.position; 
if(Distance.sqrMagnitude>minDistance*minDistance) 
{ 
    myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * moveSpeed * Time.deltaTime); 
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime; 
} 
+0

답장을 보내 주셔서 감사합니다! 나는 Unity와 C#에 익숙하지 않고 이것을 이해하려고 노력하고 있지만, 나는 이것을 작동시킬 수 없다! 컨텍스트에 minDistance가 있어야합니다. 이것을 float으로 추가합니까? 감사합니다! –

+0

i 사용 public float minDistance = 1f; 개체가 캐릭터를 따라 가며 플레이어 만이 정말로 플레이어와 멀리 떨어져 있습니다. 어떻게 해결할 수 있습니까? 미안해! –

+0

실제로 모든 좋은 생각, 도와 줘서 고마워! :) –

관련 문제