• Advertisement
Sign in to follow this  

Unity C# NavMeshAgent and Jump direction

This topic is 678 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello guys, I'm introducing myself in unity and c# and I'm facing my very first ..."complicated situation".

 
Let me explain what I'm trying to achieve; as a newbie the first thing I'm doing is trying to (little by little) understand one language at least so I started with C# and sticking to it for long time unless I need something very specific and impossible in C#.. and I don't even know if that could eventually happen.
 
So here is my first question, in the script that I'm going to paste down below, please tell me if I can avoid repeating too many lines at the end, thanks :D.
 
Then, the main thing that leads me to be here asking is the next situation, with a terrain as ground, a cube based player which moves along the terrain using a Nav Mesh Agent and a click to move script, I want this player to be able to perform his jump following the direction he was already moving before the jump.
 
Right now, my player just stop moving at all and jumps 100% vertically, just because I told him to do it as I don't know how to set x, and z) values of the Vector3 in a way that follows his previous movement direction.
 
Im currently deactivating the NavMesh Agent just before the jump as with it active the player is just permanently stuck in the floor. If there is a better way to do jumps with this agent always enabled I would really love to know it.
 
Here is the script which I'm working with, any help would be really appreciated, thanks in advance and love from Spain.
 
using UnityEngine;
using System.Collections;
 
public class Jump : MonoBehaviour
{
    private bool onGround;
    private float jumpPressure;
    private float minJump;
    private float maxJumpPressure;
    private Rigidbody rb;
    NavMeshAgent navAgent;
 
void Start ()
    {
        onGround = true;
        jumpPressure = 0f;
        minJump = 0.5f;
        maxJumpPressure = 2f;
        rb = GetComponent<Rigidbody>();
        navAgent = GetComponent<NavMeshAgent>();
    }
 
void Update ()
    {
        if(onGround)
        {
            //holding jump button//
            if (Input.GetButton("Jump"))
            {
                if(jumpPressure < maxJumpPressure)
                {
                    jumpPressure += Time.deltaTime * 12f;
                }
                else
                {
                    jumpPressure = maxJumpPressure;
                }
            }
 
            //not holding jump button anymore//
            else
            {
                //jump//
                if(jumpPressure > 0f)
                {
                    onGround = false;
                    jumpPressure = jumpPressure + minJump;
                    rb.isKinematic = false;
                    rb.useGravity = true;
                    navAgent.enabled = false;
                    rb.AddForce(new Vector3(0f, //this X value must be [-2, 2] depending of the direction of previous path
                                            jumpPressure,
                                            0f), //this z value must be [-2, 2] depending of the direction of previous path
                                            ForceMode.Impulse);
                    rb.AddRelativeForce(new Vector3(0f,  //this X value must be [-2, 2] depending of the direction of previous path
                                                    jumpPressure,
                                                    0f), //this z value must be [-2, 2] depending of the direction of previous path
                                                    ForceMode.Impulse);
                    jumpPressure = 0f;
                    //rb.velocity = new Vector3(0f, jumpPressure, 0f);
                    //onGround = false;
 
                }
            }
        }
}
 
    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.CompareTag("Ground"))
        {
            onGround = true;
            navAgent.enabled = true;
            rb.isKinematic = false;
            rb.useGravity = true;
            print ("On ground");
        }
        if (FindObjectOfType<TerrainCollider>())
        {
            onGround = true;
            navAgent.enabled = true;
            rb.isKinematic = false;
            rb.useGravity = true;
            print("On ground");
        }
    }
}
***I think that the point here is to calculate these X and Z values using the position I clicked to move and the position the player currently is, so we could determine if each one is a positive or negative movement in it's axis in order to aim that given direction, and then make the value closer or further to 2 depending of the "jumpPressure" which is set to a maximum of 2 units.
 
BUT I HAVE NO DAMN IDEA OF HOW TO DO/TYPE SUCH A THING!!!!!!!!!!!
 
Here is the script I'm using to move my player as I think it could be hiding an useful key for the wise ones willing to help me  :P
 
Player movement script:
 
using UnityEngine;
using System.Collections;
 
public class FaceMouse : MonoBehaviour
{
    public float speed;
    Vector3 position;
    int floorMask;
 
 
    NavMeshAgent navAgent;
 
    //ASAP
    void Awake()
    {
        floorMask = LayerMask.GetMask("floor");
    }
 
    // Use this for initialization
    void Start()
    {
        navAgent = GetComponent<NavMeshAgent>();
    }
 
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButton (1))
        {
            Move();
        }
    }
 
    //This method handles player movement.
    void Move()
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
        if (Physics.Raycast(ray, out hit, 200, floorMask))
         {
            position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
            Quaternion newRotation = Quaternion.LookRotation(position-transform.position, Vector3.up);
            newRotation.x = 0f;
            newRotation.z = 0f;
            transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 15);
            navAgent.SetDestination(hit.point);
         }
        
    }
}
Thank in advance guys.

 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement