how to add a jump/double jump to a free form directional mecanim controller with scripting

Please help just stuck on this part on my script for my mecanim controller I need to add a jump / double jump to this

using UnityEngine;
using System.Collections;

public class Movement : MonoBehaviour {

    Animator anim;

    bool grounded = false;
    public Transform groundCheck;
    float groundRadius = 0.2f;
    public LayerMask whatIsGround;


    // Use this for initialization
    void Start () {
     
        anim = GetComponent<Animator> ();
    }

    void FixedUpdate (){

        grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
        anim.SetBool ("Ground", grounded);

    }

    // Update is called once per frame
    void Update () {

        float input_x = Input.GetAxisRaw ("Horizontal");
        float input_z = Input.GetAxisRaw ("Vertical");

        bool IsWalking = (Mathf.Abs (input_x) + Mathf.Abs (input_z)) >0;
        anim.SetBool ("IsWalking", IsWalking);
        if (IsWalking)
        {
            anim.SetFloat ("x", input_x);
            anim.SetFloat ("z", input_z);

            transform.position += new Vector3(input_x, 0f, input_z).normalized * Time.deltaTime;
            if (grounded && Input.GetKeyDown (KeyCode.Space))
            {
                anim.SetBool ("Ground",false);
            }
        }
    }
}

Any suggestions?

try this, it works perfectly for 2D games, but it should work for 3D as well.

You’ll need to create a float called “jumpHeight” to control… well I think it’s pretty obvious xD.

if (grounded && Input.GetKeyDown(Keycode.Space))
{
     anim.SetBool("Ground", false);
     GetComponent<Rigidbody>().AddForce(Vector3.up * jumpHeight, ForceMode.Impulse);
}

OK ill give t a shot :slight_smile:

Let me know if it works!

Also by the way It’s for my 3d game so I’m trying to figure this out if you have another method I’m all ears :slight_smile:

Did that method not work?

nah it gave me some errors
here:
error CS1503: Argument #1' cannot convert object’ expression to type UnityEngine.Vector3' The best overloaded method match for UnityEngine.Rigidbody.AddForce(UnityEngine.Vector3, UnityEngine.ForceMode)’ has some invalid arguments
Operator *' cannot be applied to operands of type UnityEngine.Vector2’ and `bool’