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
1
Question by vali_unity656 · Feb 15, 2021 at 10:39 AM · rigidbody2ddirection

How do I get the direction of a moving object?

Hello, how do I get the direction of a moving object in Unity2D? I have already tried with velocity, but failed. My second attempt was to create a vector between the point of the previous update and the current position, but the values were so small that the vector was not 0 only every few updates. Are there other ways?

Comment
Add comment · Show 2
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 AbandonedCrypt · Feb 15, 2021 at 10:49 AM 1
Share

Please post the code of your moving object. That should already use a direction to move.

avatar image logicandchaos · Feb 15, 2021 at 05:04 PM 0
Share

how did you check with velocity? Are you moving the object with rigidbody and force? rb.velocity.normalized; should get the direction of the rb. Check the inspector you can monitor the velocity of the object, if it is 0 then you are moving your object without physics. In that case I would look at how you move your objects so that you can devise away to get the direction. If you are making the object move in a direction you should be able to get that direction from there.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by MUG806 · Feb 15, 2021 at 03:05 PM

How is the object being moved? It sounds like you might be manually moving it every few frames if you aren't getting non-zero values with velocity or comparing positions. If that is the case then your object wont be moving on most frames, so you cant really measure its movement reliably, and you'd be better off moving it differently. If you cant fix that, you could try using the current facing of the object using transform.forward, transform.up or transform.right? Or possibly something similar to what you tried with comparing positions, but only change the stored previous position if it changes, then you will get the last direction the object moved:

 public class MoveDirectionTracker : MonoBehaviour {
     Vector3 previousPosition;
     Vector3 lastMoveDirection;
     private void Start() {
         previousPosition = transform.position;
         lastMoveDirection = Vector3.zero;
     }
 
     private void FixedUpdate() {
         if(transform.position != previousPosition) {
             lastMoveDirection = (transform.position - previousPosition).normalized;
             previousPosition = transform.position;
         }
     }
 }
Comment
Add comment · 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
0

Answer by vali_unity656 · Feb 15, 2021 at 05:21 PM

My idea is to "flick" the ball across the screen by dragging the mouse, like a slingshot.

 void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             startpoint = camera.ScreenToWorldPoint(Input.mousePosition);
             startpoint.z = 23;
             rb.velocity = Vector2.zero;
             rb.gravityScale = 0;
 
         }
         if (Input.GetMouseButtonUp(0))
         {
             endpoint = camera.ScreenToWorldPoint(Input.mousePosition);
             endpoint.z = 23;
             ballforce = new Vector2(startpoint.x - endpoint.x, startpoint.y - endpoint.y);
             rb.gravityScale = Gravitys;
             rb.AddForce(ballforce * BallPower, ForceMode2D.Impulse);
         }
 
         if (!Input.GetMouseButton(0))
 
         {
             float angle = Mathf.Atan2(transform.position.y - mPosition.y, transform.position.x - mPosition.x);
             if (transform.position - mPosition != new Vector3(0, 0, 0))
             {
                 transform.rotation = Quaternion.Euler(0, 0, angle);
                 Vector3 Test = transform.position - mPosition;
 
                 UnityEngine.Debug.Log(Test);
                 mPosition = transform.position;
             }
 
 
         }
     }

But when I want to get the direction of the Ball, to rotate it that I can squeeze it in that way, the angle is every time 0 degrees, because the current position and the previous position is 0.

Comment
Add comment · 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

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

117 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

Related Questions

Using direction and Rigidbody2D.AddForce() to move towards object. 1 Answer

AI movement with rigidbody2d 0 Answers

Move a rigidbody to its facing direction 1 Answer

Rigidbody2D velocity in random direction with exceptions 1 Answer

Issue with direction and rigibody2D 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