• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I add force to a RigidBody2D? 2 Answers

How do you make a car drift in unity 2D 1 Answer

OnTriggerEnter2D(Collider2D other) 2 Answers

Way to achieve 3 lane mechanism in a 2D game 0 Answers

Rigidbody and Collision choices for 2D Adventure 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges