Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Please Assist: Player Movement

Discussion in 'Physics' started by kdogjake, Nov 2, 2020.

  1. kdogjake

    kdogjake

    Joined:
    Oct 29, 2020
    Posts:
    2
    Hi. I'm currently working on my first 3D unity game and am having some trouble with the character movement. The player cannot change their velocity on the X and Z axis while in the air, and I wanted to change that. I've tried removing the if statement detecting that the player is grounded, but for some reason that makes the player unable to jump. Please advise.

    Code (CSharp):
    1. CharacterController controller = GetComponent<CharacterController>();
    2.         AudioSource jumpSound = GetComponent<AudioSource>();
    3.         // is the controller on the ground?
    4.  
    5.         if (controller.isGrounded)
    6.         {
    7.             isRunning = false;
    8.             //Feed moveDirection with input.
    9.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    10.             moveDirection = transform.TransformDirection(moveDirection);
    11.             //Multiply it by speed.
    12.             if (Input.GetKey(KeyCode.LeftControl) && moveDirection.x != 0)
    13.             {
    14.                 moveDirection *= speed*2;
    15.                 isRunning = true;
    16.             } else
    17.             {
    18.                 moveDirection *= speed;
    19.             }
    20.            
    21.             //Jumping
    22.             if (Input.GetButton("Jump") && controller.isGrounded)
    23.             {
    24.                 moveDirection.y = jumpSpeed;
    25.                 jumpSound.Play();
    26.                 isRunning = false;
    27.             } else
    28.             {
    29.                 moveDirection.y = 0.1f;
    30.             }
    31.         }
    32.         //Applying gravity to the controller
    33.         moveDirection.y -= gravity * Time.deltaTime;
    34.         //Making the character move
    35.         controller.Move(moveDirection * Time.deltaTime);
     
  2. wztk

    wztk

    Joined:
    Feb 25, 2020
    Posts:
    41
    Hi,

    The isGrounded check is essential for Jump to work, because otherwise you either would be able to jump indefinitely or, as in your case, not jump at all.
    However, it's included twice in your code when it's not necessary, so you can remove it in line 22.
    Also, move direction is only assigned to when character is on the ground, but when it's jumping it doesn't read horizontal and vertical axes, so you need to create an else case or a general case that works regardless of whether the player is grounded or not.

    Kind regards.
     
    Last edited: Nov 2, 2020
  3. kdogjake

    kdogjake

    Joined:
    Oct 29, 2020
    Posts:
    2
    I'm unsure as to why, but when I've tried this my character has been unable to jump. I attempted to move the horizontal movement outside of the isGrounded check, but it resulted in the same thing of my character being unable to jump.
     
  4. wztk

    wztk

    Joined:
    Feb 25, 2020
    Posts:
    41
    Can you show the code snippet that does that ?