• 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 LilGumba · Apr 27, 2019 at 07:47 PM · 2dmovementtopdown

How do I detect when my player is moving in unity 2d?

I am trying to make a top down game with movement like in the game Binding of Isaac. I want my player to move all 4 directions facing one way, but when he shoots he faces the direction of where he is shooting, not the direction he is moving towards. I can figure most parts out, but I am not sure how to detect when he is moving in ANY direction so I can set a float "speed" to how fast he is moving and make it in the player animator controller if the speed is greater than 0.1 he will play a moving animation. I know I haven't explained it very well, so I'll just say I am trying to figure out how to set a float "speed" to however fast the player is moving, and it doesn't matter what direction. Anyone have any answers? Thanks!

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

Answer by Jack-Mariani · Apr 30, 2019 at 06:32 PM

You may check the direction of a Rigidbody2D by checking its velocity.

 ThisRigidbody2D.velocity;    //catch the velocity, as a vector

 ThisRigidbody2D.velocity.magnitude; //calculate the speed, always equal or more than 0

To let the trasform follow the rigidbody you may apply this:

  [RequireComponent(typeof(Rigidbody2D))]
  public class Rigidbody2DLookAt : MonoBehaviour
  {
     //to decide the minimal speed, if the rigidbody is moving slower than that we stop
     public float speedThreshold = 0.1f;
     //the offset if you need to adjust the rotation
     public float offset = 90f;

     //the rigidbody
     private Rigidbody2D _rigidbody2D;

     //cache the rigidbody at start (or awake)
     private void Start() { _rigidbody2D = GetComponent<Rigidbody2D>(); }

     private void Update()
     {
         //gets the velocity as a vector
         var velocity = _rigidbody2D.velocity;

         //stops following the rigidbody rotation if 'moving too slow
         if (velocity.magnitude <= speedThreshold) return;

         //calculate the angle and rotate the transform accordingly
         var angle = Mathf.Atan2(velocity.y, velocity.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + offset));
     }
 }
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 LilGumba · May 01, 2019 at 03:07 PM 0
Share

Thanks for the answer. So if I am using a rigidbody2D, how exactly can I say "if the velocity is greater than 0 then....."?

I have tried using rb.velocity.magnitude but it just doesn't seem to work ;(

avatar image Jack-Mariani LilGumba · May 01, 2019 at 05:09 PM 0
Share

rigidbody.velocity.magnitude sends back only the speed without the direction.

Try applying this:

 var velocity = rigidbody.velocity;
 if(velocity.magnitude > 0.1) 
    this.transform.rotation = Quaternion.LookRotation(velocity, Vector2.right);

Otherwise tomorrow I'll check other solutions.

**EDIT: I've just updated the answer. The code in the answer works better for 2d. You may ignore this.

avatar image Jack-Mariani LilGumba · May 02, 2019 at 07:18 AM 0
Share

I've adjusted the answer after your comment and tested it on a scene. It is working on my side. Let me know if it works for you too.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

244 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

Top down camera that follows the player and rotates with the player while remaining constrained to world bounds 0 Answers

Enemy Movement Help 3 Answers

Tile based movement collisions 0 Answers

Top-Down Movement in Unity 1 Answer


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