Search Unity

Question PlayerController

Discussion in 'Scripting' started by Luke3671, Jul 11, 2020.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Hey
    With this script is there a way to add isGround so you can spam jump?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. //Make sure your change PlayerController to your C# file name!
    6.  
    7. public class PlayerController : MonoBehaviour
    8. {
    9.  
    10.     static Animator anim;
    11.     public float speed = 10.0F;
    12.     public float rotationSpeed = 100.0f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         anim = GetComponent<Animator>();
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         float translation = Input.GetAxis("Vertical") * speed;
    24.         float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
    25.         translation *= Time.deltaTime;
    26.         rotation *= Time.deltaTime;
    27.         transform.Translate(0, 0, translation);
    28.         transform.Rotate(0, rotation, 0);
    29.  
    30.         if (Input.GetButtonDown("Jump"))
    31.         {
    32.             anim.SetTrigger("isJumping");
    33.         }
    34.  
    35.         if (translation != 0)
    36.         {
    37.             anim.SetBool("isRunning", true);
    38.             anim.SetBool("isIdle", false);
    39.         }
    40.         else
    41.         {
    42.             anim.SetBool("isRunning", false);
    43.             anim.SetBool("isIdle", true);
    44.  
    45.         }
    46.     }
    47. }
    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.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Jump : MonoBehaviour {
    7.  
    8.     public Vector3 jumpVector;
    9.  
    10.     // Update is called once per frame
    11.     void Update() {
    12.         if (Input.GetKeyDown(KeyCode.Space)) {
    13.             GetComponent<Rigidbody>().AddForce(jumpVector, ForceMode.VelocityChange);
    14.             }
    15.         }
    16. }
    17.  
    Thank you if you can help.
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    I checked on google, and spam jumping is jumping while still in the air?
     
  3. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    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.
     
  4. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    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
     
    Luke3671 likes this.