如何使用Nav Mesh Agent跳转播放器模型

时间:2017-10-09 14:47:33

标签: unity3d 3d navmesh

我在带有导航网格代理的播放器模型上有一个程序代码,允许它在点击时环游世界,但我试图让它跳跃并且似乎无法实现它。

这是我的代码,不知道要添加或删除的内容

public class WorldInteraction : MonoBehaviour {
NavMeshAgent playerAgent;

// Use this for initialization
void Start () {
    playerAgent = GetComponent<NavMeshAgent> (); //instantiate the nav mesh to PlayerAgent
}

// Update is called once per frame
void Update () {
    if (Input.GetMouseButtonDown (0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()) //condition if the postion is being clicked on a UI is veung clicked
    {
        GetInteraction (); //call interaction method
    }
    if (Input.GetMouseButtonDown (1) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject ()) 
    {
        transform.Translate (Vector3.up);
    }
}
void GetInteraction(){ //this method gets the ray or point clicked and move the player to that point
    Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition); //get the point clicked in the world
    RaycastHit interactionInfo; //keeps track of the point clicked
    if (Physics.Raycast (interactionRay, out interactionInfo, Mathf.Infinity)) //get the point clicked, store it in InteractionInfo and make sure its not out of range by mathf 
    { 
        GameObject interactedObject = interactionInfo.collider.gameObject;
        if (interactedObject.tag == "Interactable Item") //check if the item point selected is interacrable(cant be move over)
        {
            interactedObject.GetComponent<Interactable> ().MoveToInteraction (playerAgent); //move playerAgent to the Interactable item, so they could interact(its calling the movetoInteractable method in Interactable class).
        } else {
            playerAgent.stoppingDistance = 0;
            playerAgent.destination = interactionInfo.point; //if its a movable point, player destination is set to that point
        }
    } 

}

}

1 个答案:

答案 0 :(得分:0)

NavMeshAgent控制所有方向的对象,因此它将覆盖您的跳跃尝试。使NavMeshAgent使对象成为空对象的子对象,然后向上翻译空对象。希望这会有所帮助。