Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Jumping when not intended with Controller

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

  1. SuXuK

    SuXuK

    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. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,965
    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. SuXuK

    SuXuK

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

    LurkingNinjaDev

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