0

我在带有 Nav Mesh Agent 的 Player 模型上有一个程序代码,它允许它在单击时环游世界,但我试图让它跳跃并且似乎没有实现它。

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

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
        }
    } 

}

}

4

1 回答 1

0

The NavMeshAgent controls the object in all directions so it will just override your attempt to jump. Make the object with the NavMeshAgent a child of an empty object and just Translate the empty object upwards. Hopefully that helps.

于 2017-10-09T16:00:48.180 回答