Help with good coding practices and player Nav Mesh Agent Jumps. Thanks.

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.

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, 
                                        jumpPressure,
                                        0f),
                                        ForceMode.Impulse);
                rb.AddRelativeForce(new Vector3(0f,
                                                jumpPressure,
                                                0f),
                                                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 had a syntax error the first time I tried but now at the end both “if” statements are together with the or thing “||”

But still need help with the main thing… :frowning:

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.
I could paste the script I’m using to move my player if you guys request.