Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Jumping when not intended with Controller

Discussion in 'Scripting' started by IlyasKaya73, Dec 8, 2018.

  1. IlyasKaya73

    IlyasKaya73

    Joined:
    Dec 8, 2018
    Posts:
    2
    My problem is that when I press A on my controller, and when I hit my resume button I would jump, I know I can either change which button to trigger my jump or what to press to resume but that won't do for me xD

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class MovePlayer : MonoBehaviour
    7. {
    8.  
    9.     public float speed = 10.0F;
    10.     public float gravity = 20.0F;
    11.     private Vector3 moveDirection = Vector3.zero;
    12.     public float speedH = 2.0f;
    13.     private float rotationX = 0.0f;
    14.     private float jumpSpeed = 0.0F;
    15.     public PlayerHealth playerHealth;
    16.  
    17.     void Update()
    18.     {
    19.         rotationX += speedH * Input.GetAxis("Mouse X");
    20.  
    21.         CharacterController controller = GetComponent<CharacterController>();
    22.         if (controller.isGrounded)
    23.         {
    24.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    25.             moveDirection = transform.TransformDirection(moveDirection);
    26.             moveDirection *= speed;
    27.             if (Input.GetButton("Jump"))
    28.                 moveDirection.y = jumpSpeed;
    29.  
    30.         }
    31.         moveDirection.y -= gravity * Time.deltaTime;
    32.         controller.Move(moveDirection * Time.deltaTime);
    33.         if (PauseMenu.GameIsPaused == false | playerHealth.CurrentHealth <= 0)
    34.         {
    35.             transform.localEulerAngles = new Vector3(0.0f, rotationX, 0.0f); // Moving camera on Vector 3
    36.             jumpSpeed = 8;
    37.         }
    38.     }
    39. }
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,437
    Using GetButton for jumping (or menu if you're using it as well) is a bad idea, one time events (like initiate a jump) should put on GetButtonDown. The GetButton will be true while you're holding the button down. So it will continuously jumping.

    With that said, you may still have a problem if your movement is executing after the menu (execution order). You can set the script execution order in the settings or you do not set this to false immediately. Create a coroutine which will yield until the end of the frame and set the variable there to false.
    GetButtonDown is true only in the next whole frame after hitting the button physically.
     
  3. IlyasKaya73

    IlyasKaya73

    Joined:
    Dec 8, 2018
    Posts:
    2
    Just making it to "GetButtonDown" worked, I'm so stupid and thanks.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,437
    You're welcome. And you're not stupid. We all have been there and made the same mistakes. ;)