Search Unity

How do i add animations to the Horizontal axis?

Discussion in 'Getting Started' started by MYM_Chapa, Jan 8, 2023.

  1. MYM_Chapa

    MYM_Chapa

    Joined:
    Dec 6, 2022
    Posts:
    15
    I was trying to make a good movement system for my first game and I tried to implement animations to the game but I don't know how, this is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class PlayerControler : MonoBehaviour
    7. {
    8.     Rigidbody2D rb;
    9.     public float velocity;
    10.     public float jumpVelocity;
    11.     [SerializeField] LayerMask groundLayer;
    12.     float timeInTheAir;
    13.     public float maxJumpTme;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rb = gameObject.GetComponent<Rigidbody2D>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         //Move with "A" and "D"
    25.         rb.velocity = new Vector3(Input.GetAxisRaw("Horizontal") * velocity, rb.velocity.y, 0);
    26.        
    27.  
    28.      
    29.  
    30.  
    31.         //Raycast
    32.         RaycastHit2D raycastGround = Physics2D.Raycast(transform.position, Vector2.down, 0.25f, groundLayer);
    33.  
    34.         if(raycastGound)
    35.         {
    36.             maxJumpTme = 0;
    37.         }
    38.         //Jumping with Space
    39.         if(Input.GetKey(KeyCode.Space) && maxJumpTme < 0.2f)
    40.         {
    41.             maxJumpTme += Time.deltaTime;
    42.             rb.velocity = new Vector2(rb.velocity.x, jumpVelocity);
    43.         }
    44.            
    45.     }
    46.  
    47. }
    48.  
    also i want to fix my raycast because its broken, the problem is when i jump i only can jump once and the i cant jump anymore.