Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

(2d) character cant move left or right

Discussion in 'Scripting' started by Rhythmatica, Mar 1, 2021.

  1. Rhythmatica

    Rhythmatica

    Joined:
    Mar 1, 2021
    Posts:
    12
    i dont know programming language, but i understand how the process works
    character can jump/fall with animation, and the animations of moving left and right work when i press a/d, but no movement occurs.
    I am getting no compiler errors, so i think a setting is just not working.




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class movement : MonoBehaviour
    5. {
    6.    public Rigidbody2D rb;
    7.    private Animator anim;
    8.    private enum State {idle, running, jumping, falling}
    9.    private State state = State.idle;
    10.    private Collider2D coll;
    11.    [SerializeField] private LayerMask ground;
    12.    [SerializeField] private float speed = 10;
    13.    [SerializeField] private float jumpForce = 10f;
    14.     private void Start()
    15.         {
    16.         rb = GetComponent<Rigidbody2D>();
    17.         anim = GetComponent<Animator>();
    18.         coll = GetComponent<Collider2D>();
    19.         }
    20.     private void Update()
    21.     {
    22.         float hDirection = Input.GetAxis("Horizontal");
    23.         if (hDirection < 0)
    24.         {
    25.             rb.velocity = new Vector2(-speed, rb.velocity.y);
    26.             gameObject.GetComponent<SpriteRenderer>().flipX = true;
    27.         }
    28.         else if (hDirection > 0)
    29.         {
    30.             rb.velocity = new Vector2(speed,rb.velocity.y);
    31.             gameObject.GetComponent<SpriteRenderer>().flipX = false;
    32.         }
    33.         else
    34.         {  
    35.         }
    36.         if(Input.GetButtonDown("Jump") && coll.IsTouchingLayers(ground))
    37.         {
    38.             rb.velocity = new Vector2(rb.velocity.x,jumpForce);
    39.             state = State.jumping;
    40.         }
    41.         VelocityState();
    42.         anim.SetInteger("state", (int)state);
    43.     }
    44.     private void VelocityState()
    45.     {
    46.         if(state == State.jumping)
    47.         {
    48.             if(rb.velocity.y< .1f)
    49.             {
    50.                 state = State.falling;
    51.             }
    52.  
    53.         }
    54.         else if(state == State.falling)
    55.         {
    56.             if(coll.IsTouchingLayers(ground))
    57.             {
    58.                 state = State.idle;
    59.             }
    60.         }
    61.         else if(Mathf.Abs(rb.velocity.x) > 9.5f)
    62.         {
    63.             state = State.running;
    64.         }
    65.         else
    66.         {
    67.             state = State.idle;
    68.         }
    69.     }
    70. }
     
  2. simiel7

    simiel7

    Joined:
    Dec 18, 2020
    Posts:
    7
    My first guess is the mass is too big (or the speed is too low) and the gravity itself is too big and the character cannot move because too much force pushing it down.

    Move you character upper than the ground to see it fall (or even remove the ground). And see the character falling and press your A / D key and if I'm right you will see your character moving left / right slowly. So, it will mean the script is working and the settings are not right and too much force is pushing the character down.

    You can also play with the speed for example and increase it to 1000.

    What happens when you jump and move left / right at the same time?

    Also if you do not want friction between the character and ground, you should check the PhysicsMaterial for the BoxCollider starting from here: https://docs.unity3d.com/2021.1/Documentation/Manual/class-PhysicsMaterial2D.html. You can achieve a slippery ground with it or lower friction if you wish.
     
  3. Rhythmatica

    Rhythmatica

    Joined:
    Mar 1, 2021
    Posts:
    12
    I have increased it to 1000 and nothing happened. i did it for a bit and position didnt change, even in the Transform section for player inspect. If I jump and go left/right, nothing happens
    as you see in the image i attached, my mass, gravity, and linear drag are set quite low.
    thanks for the help, but i dont know whats happening
     
  4. simiel7

    simiel7

    Joined:
    Dec 18, 2020
    Posts:
    7
    Although, it is working for me.

    Did you changed the Input Manager or any input settings?
    Could you take a screenshot what are the Horizontal settings in the Input Manager? (Edit > Project Settings > Input Manager tab).


    If the jump is working as you stated, you should try to change this line and following logic
    Code (CSharp):
    1. float hDirection = Input.GetAxis("Horizontal");
    to a specific character for example:
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.D))
    2. {
    3.     rb.velocity = new Vector2(-speed, rb.velocity.y);
    4.     gameObject.GetComponent<SpriteRenderer>().flipX = true;
    5. }
    It just for testing with a direct key instead of the Horizontal axes (using the Horizontal is a good solution, maybe it was misconfigured).