- Home /
 
 
               Question by 
               vancoredieddepression · Mar 21 at 05:10 PM · 
                2d gameunity 2d2d-physics  
              
 
              Does anyone know how to fix this unity 2d movement isssue? X axis movement not working
I was trying to make a wall jump ability on my 2d mobile game, but I noticed a strange issue. If I try to use add force or rb.velocity to make the rigid body move left or right, it doesn't work. The only movement on the x axis that works are the move left and right arrows. Here is my entire movement script.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerMovement : MonoBehaviour
 {
     [Header("Movement")]
     public float moveSpeed;
     private float moveX;
 
     public bool facingRight;
     public float lookDirection;
 
     [Header("Drag")]
     public float groundDrag = 5f;
     public float airDrag = 6f;
     public float wallDrag = 3f;
 
     [Header("Ground Detection")]
     [SerializeField] private LayerMask jGround;
     [SerializeField] private Transform feet;
     [SerializeField] float circleRadius;
     private CapsuleCollider2D coll;
 
     [Header("Jump")]
     [SerializeField] float wallJumpAngle = 45f;
     public float jumpForce;
     public string jumpButton = "Jump";
 
     [Header("Walll Jump")]
     [SerializeField] private LayerMask jWall;
     [SerializeField] float wallSlideSpeed = 0.3f;
     [SerializeField] float wallDistance = 0.5f;
     [SerializeField] float wallJumpForce = 1f;
     bool isWallSliding = false;
     float jumpTime;
     RaycastHit2D wallCheckHit;
 
     [Header("Crouching")]
     [SerializeField] public float crouchSpeed = 5;
     [SerializeField] float crouchYScale;
     [SerializeField] float crouchPushSpeed;
     private float crouchMultiplier = 1;
     private float startYScale;
     private float playerYScale;
     private bool isCrouch;
 
 
     private Rigidbody2D rb;
 
     void Start()
     {
         rb = gameObject.GetComponent<Rigidbody2D>();
         coll = GetComponent<CapsuleCollider2D>();
 
         startYScale = transform.localScale.y;
         playerYScale = startYScale;
     }
 
     void Update()
     {
         ControlDrag();
         ControlSpeed();
         CheckWall();
 
         print(IsGrounded());
 
         moveX = SimpleInput.GetAxis("Horizontal");
 
         rb.velocity = new Vector2(moveX * moveSpeed * crouchMultiplier, rb.velocity.y);
 
         if (Input.GetKeyDown(KeyCode.Space))
         {
             Jump();
         }
 
         if (SimpleInput.GetAxis("Horizontal") > 0.5)
         {
             facingRight = true;
         }
         else if (SimpleInput.GetAxis("Horizontal") < -0.5)
         {
             facingRight = false;
         }
 
         if (facingRight)
         {
             transform.localScale = new Vector2(1, playerYScale);
             lookDirection = 1;
         }
         else if (!facingRight)
         {
             transform.localScale = new Vector2(-1, playerYScale);
             lookDirection = -1;
         }
     }
 
     void ControlDrag()
     {
         if (IsGrounded())
         {
             rb.drag = groundDrag;
         }
         else if (!IsGrounded() && !isWallSliding)
         {
             rb.drag = airDrag;
         }
     }
 
     void ControlSpeed()
     {
         if (isCrouch == true && IsGrounded())
         {
             crouchMultiplier = crouchSpeed;
         }
         else
         {
             crouchMultiplier = 1;
         }
     }
 
     public void CheckWall()
     {
         if (rb.velocity.x > 0.1)
         {
             wallCheckHit = Physics2D.Raycast(transform.position, Vector2.right, wallDistance, jWall);
             Debug.DrawRay(transform.position, new Vector2(wallDistance, 0), Color.white);
         }
         else if (rb.velocity.x < -0.1)
         {
             wallCheckHit = Physics2D.Raycast(transform.position, Vector2.left, wallDistance, jWall);
             Debug.DrawRay(transform.position, new Vector2(-wallDistance, 0), Color.white);
         }
         
         //End of Wall Check
 
         if (wallCheckHit && !IsGrounded())
         {
             Debug.Log("WORKING");
             isWallSliding = true;
         }
         else
         {
             isWallSliding = false;
         }
 
         //End of Bools
 
         if (isWallSliding)
         {
             if (rb.velocity.y < -wallSlideSpeed)
             {
                 rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
             }
         }
     }
 
     public void Jump2()
     {
         rb.velocity = new Vector2(moveSpeed * crouchMultiplier, rb.velocity.y);
     }
     public void Jump()
     {
         if (IsGrounded())
         {
            rb.velocity = Vector2.up * jumpForce;
         }
         if (isWallSliding)
         {
             Debug.Log("Jump");
             rb.AddForce(Vector2.right * wallJumpForce, ForceMode2D.Impulse);
         }
     }
 
     public void Crouch()
     {
         if (facingRight || !facingRight)
         {
             playerYScale = crouchYScale;
             rb.AddForce(Vector2.down * crouchPushSpeed, ForceMode2D.Impulse);
             isCrouch = true;
         }
     }
 
     public void StopCrouch()
     {
         playerYScale = startYScale;
         isCrouch = false;
     }
 
     private bool IsGrounded()
     {
         return Physics2D.CircleCast(feet.position, circleRadius, Vector2.down, 0.1f, jGround);        
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
OnTriggerEnter2D(Collider2D other) 2 Answers
Way to achieve 3 lane mechanism in a 2D game 0 Answers
How do I add force to a RigidBody2D? 2 Answers
How do you make a car drift in unity 2D 1 Answer
Creating 'redstone' like wires 1 Answer