PlayerController

Hey
With this script is there a way to add isGround so you can spam jump?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

//Make sure your change PlayerController to your C# file name!

public class PlayerController : MonoBehaviour
{

    static Animator anim;
    public float speed = 10.0F;
    public float rotationSpeed = 100.0f;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        float translation = Input.GetAxis("Vertical") * speed;
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        transform.Translate(0, 0, translation);
        transform.Rotate(0, rotation, 0);

        if (Input.GetButtonDown("Jump"))
        {
            anim.SetTrigger("isJumping");
        }

        if (translation != 0)
        {
            anim.SetBool("isRunning", true);
            anim.SetBool("isIdle", false);
        }
        else
        {
            anim.SetBool("isRunning", false);
            anim.SetBool("isIdle", true);

        }
    }
}

I have this extra code in the background on my player to make the character jump with force just without isGrounded you can spam jump.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Jump : MonoBehaviour {

    public Vector3 jumpVector;

    // Update is called once per frame
    void Update() {
        if (Input.GetKeyDown(KeyCode.Space)) {
            GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.VelocityChange);
            }
        } 
}

Thank you if you can help.

I checked on google, and spam jumping is jumping while still in the air?

Because in the code isGrounded is not in there I can spam jump & my char will just go to the sky. Is there a way to add IsGrounded to the script so the player can only jump once when on the ground & not spam jump? Is it a bool or a Trigger that i need in the animator to make this work & what would the code look like? I’m new to this to really learning if there any tutorials you know that would also be great.

thank you.

See this lesson from Unity Learn’s “Create with Code” tutorial. Scroll down to Part 7. Prevent player from double-jumping.

Lesson 3.1 - Jump Force

1 Like