Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Character gravity gets extremely low and he is not able to jump when a animation gets added

Discussion in 'Animation' started by drakkwyr001, Jul 7, 2019.

  1. drakkwyr001

    drakkwyr001

    Joined:
    Jul 1, 2019
    Posts:
    15
    Heres the settings of the animation (running animation):
    Duvida.PNG
    Duvida 2.PNG
    Heres the code where jumping is:
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     //Variables
    4.  
    5.     public float speed = 0.5f;
    6.     public float sprintSpeed = 2.5f;
    7.     private Rigidbody rigidB;
    8.     private float midJump = 1f;
    9.     public float rotSpeed = 2;
    10.    
    11.     void Start()
    12.     {
    13.         rigidB = GetComponent<Rigidbody>();
    14.  
    15.     }
    16.     void Update()
    17.     {
    18.  
    19.         //Character rotation with A and D
    20.  
    21.         var z = Input.GetAxis("Horizontal") * rotSpeed;
    22.  
    23.         //Character movement with W and S
    24.  
    25.         var y = Input.GetAxis("Vertical") * speed;
    26.  
    27.         //Sprint if player clicks RightShift
    28.  
    29.         if (Input.GetKey(KeyCode.RightShift))
    30.             y = y * sprintSpeed;
    31.  
    32.  
    33.         transform.Translate(0, -y, 0);
    34.         transform.Rotate(0, 0, -z);
    35.  
    36.  
    37.  
    38.         //Character Jump
    39.  
    40.  
    41.         if (midJump == 1 && Input.GetKeyDown(KeyCode.Space))
    42.         {
    43.             rigidB.velocity = new Vector3(0, 45, 0);
    44.             midJump = 2;
    45.  
    46.         }
    47.         else if (rigidB.velocity.y == 0)
    48.             midJump = 1;
    49.  
    50.     }
    51. }
    52.