0

i have a program code on a Player model with Nav Mesh Agent that allows it to walk around the world when clicked but am trying to make it jump and haven't seem to achieve it.

this is my code, don't know what to add or remove

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

}

}

jmarkmurphy
  • 11,030
  • 31
  • 59

1 Answers1

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.

  • the player model has the navmeshagent on it and its a child of an empty object but yet it didnt move... Can u help me with a code or something related? cause its not working for me – Anthony Praise Ofume Oct 10 '17 at 10:56
  • i tried this, transform.Translate(Vector3.up * Time.deltaTime * 50, Space.World); – Anthony Praise Ofume Oct 10 '17 at 11:21
  • it jumped but was so fast and was not noticed – Anthony Praise Ofume Oct 10 '17 at 11:21
  • You can try putting the value of 50 into a variable and testing different values until you get something that feels right. There's plenty of different ways to make an object "jump" in Unity. The key is to use the empty object as the NavmeshAgent won't let you overwrite the transform on the object it's attached to. –  Oct 11 '17 at 16:15