Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 sajithsathes · Oct 11, 2020 at 02:36 PM · movementvelocity2d-physicsverticalhorizontal movement

Input.GetAxisRaw isnt being called?

I have a script that takes user input from the horizontal and vertical axes via Update, stores them in 2 floats, moveX and moveY, and calculate velocity of the rigidbody2D via FixedUpdate. the calculation is done in a separate function. I first calculate the velocity along the x axis and then the velocity along the y. While the Y is getting registered, the x isnt. I swapped up the order in which the calculation is done and the condition swapped as well; now x is being registered while y isnt. Help!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public Rigidbody2D rb2d;
     float moveX;
     float moveY;
     public float moveSpeed;
 
     private void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
     }
 
     private void Update()
     {
         moveX = Input.GetAxisRaw("Horizontal");
         moveY = Input.GetAxisRaw("Vertical");
     }
 
     private void FixedUpdate()
     {
         CalculateVelocity();
     }
 
     void CalculateVelocity()
     {
         if (moveX != 0)
         {
             rb2d.velocity = Vector2.right * new Vector2(moveX, rb2d.velocity.y) * moveSpeed;
         }
         else rb2d.velocity = Vector2.zero;
 
         if (moveY != 0)
         {
             rb2d.velocity = transform.up * new Vector2(rb2d.velocity.x, moveY) * moveSpeed;
         }
         else rb2d.velocity = Vector2.zero;
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by sztobar · Oct 11, 2020 at 02:46 PM

You multiple rb2d.velocity by either Vector2.right or transform.up. Both of these vectors resets one axis to zero. It's becuase:

Vector2.right = new Vector2(1, 0);

transform.up = new Vector2(0, 1); or transform.up = new Vector2(0, -1); (or something different if the game object is rotated)

The fix to your problem should be simple as writing something like this instead:

 rb2d.velocity = new Vector2(
   moveX * moveSpeed,
   moveY * moveSpeed
 );
Comment
Add comment · Show 3 · Share
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
avatar image sajithsathes · Oct 12, 2020 at 03:48 AM 0
Share

OK that's understandable but then why is it that only the second input (based on the order in which the code is written) is being read? And I'd usually go with the velocity way of moving things about in unity but the problem came in when I had to rotate the camera, based on which the sprites of the scene would also rotate and imitate the camera rotation. So after, say, a rotation of 90 degrees (of the camera but the sprites rotate 90 as well), the local axes and the global axes are out of sync. After another 90, they get flipped! So as to overcome this i thought I'd provide a direction to the movement script via the method in the problem. Now while the method I chose isn't respondent on one axis, after 90 degrees of rotation, there's ZERO respondent axes and after another 90, the axis that was respondent originally is still working fine!

avatar image sztobar sajithsathes · Oct 13, 2020 at 10:24 AM 1
Share

About only the second input being read - if you would run a visual studio debugger (it's initiated by clicking on Attach to Unity button there) you can see how your code is executed, which might help you understand why the code behaves that way. Let me try to visualize that by writing on comments which code lines gets executed :

  1. when moveX != 0 and moveY !=0

           if (moveX != 0) // <- is true
              {
                  rb2d.velocity = Vector2.right * new Vector2(moveX, rb2d.velocity.y) * moveSpeed; // <- this gets assigned to velocity
              }
              else rb2d.velocity = Vector2.zero;
      
              if (moveY != 0) // <- is also true
              {
                  rb2d.velocity = transform.up * new Vector2(rb2d.velocity.x, moveY) * moveSpeed; // <- this get assigned to velocity thus replacing previous condition
              }
              else rb2d.velocity = Vector2.zero;
    
    
    
  2. when moveX == 0 and moveY ==0

           if (moveX != 0) // <- is false
              {
                  rb2d.velocity = Vector2.right * new Vector2(moveX, rb2d.velocity.y) * moveSpeed;
              }
              else rb2d.velocity = Vector2.zero; // <- this gets assigned
      
              if (moveY != 0) // is false
              {
                  rb2d.velocity = transform.up * new Vector2(rb2d.velocity.x, moveY) * moveSpeed;
              }
              else rb2d.velocity = Vector2.zero; // <- this gets assigned
    
    
  3. when moveX != 0 and moveY ==0

           if (moveX != 0) // <- is true
              {
                  rb2d.velocity = Vector2.right * new Vector2(moveX, rb2d.velocity.y) * moveSpeed; // <- this gets assigned to velocity
              }
              else rb2d.velocity = Vector2.zero;
      
              if (moveY != 0) // is false
              {
                  rb2d.velocity = transform.up * new Vector2(rb2d.velocity.x, moveY) * moveSpeed;
              }
              else rb2d.velocity = Vector2.zero; // <- this gets assigned to velocity, and replaces velocity
    
    
  4. when moveX == 0 and moveY != 0

            if (moveX != 0) // <- is false
              {
                  rb2d.velocity = Vector2.right * new Vector2(moveX, rb2d.velocity.y) * moveSpeed;
              }
              else rb2d.velocity = Vector2.zero; // <- this gets assigned to velocity
      
              if (moveY != 0) // <- is true
              {
                  rb2d.velocity = transform.up * new Vector2(rb2d.velocity.x, moveY) * moveSpeed; // <- this gets assigned to velocity
              }
              else rb2d.velocity = Vector2.zero;
    
    

In summary: No matter what the value of moveX is - the condition with moveY (no matter if true or false) will replace its effects.

Hope that clears things a little.

avatar image sajithsathes sztobar · Oct 14, 2020 at 03:42 AM 0
Share

That is extremely insightful! I did come to the conclusion that only the second input was working using the vs debugger but didn't dig this deep into it. I'll see if I can make up some fix for it and post it asa I find one. Thanks again for the help!

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

205 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 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

Smooth movement using Rigidbody2d 3 Answers

2D Directional top down movement,Topdown 2d Directional Movement 0 Answers

Movement using rigidbody.velocity to apply a constant force until stop 1 Answer

helicopter horizontal movement problem C# 0 Answers

How to make the player move a fixed distance? (Tile per tile) 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