Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by MayIAsk · Feb 15, 2017 at 12:21 AM · rigidbody2dvelocityphysics2ddirectionmoveposition

MovePosition/Velocity not following a direction

Hello !

I have a quick question about Unity physics and lerping to a direction point. My rigidbody is set to non-kinematic, gravity = 0, same for angles = 0, and when I debug the magnitude of my direction is at 12 when I launch the item forward (x = 0, y = 12) and more than 12 when it's a diagonal move.

I tried everything, velocity, moveposition, clamping magnitude to 1 or 12 (in my case), normalize the direction, nothing is working the way I want. The item goes in a different direction and not the one I affect to velocity or moveposition.

 //My unique direction
 _direction = new Vector2(_mousePosX, _mousePosY).normalized;
 
 //Want the item to follow exactly this direction line
 _item.GetComponent<Rigidbody2D>().velocity = _direction;
 
 
 

This is a quick pic of my issue : alt text

I don't get why my object doesn't go follow the direction, it's like it is controlled by something else but I can't figure what.. I just want my velocity to be equals as a normal lerp like from the origin point to the direction point. But velocity seems to not act like a normal lerp, it has something that modify the final direction point to an other controlled by physic?

I have no issues with colliders, no issue to lerp the object with physics but the only issue is that the item does not exactly follow the direction line.

I searched for hours, reading all topics, can't figure what is wrong with my code. If you can help me, it'll be much appreciated. Thank you in advance.

rigidbody.png (7.5 kB)
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 toddisarockstar · Feb 16, 2017 at 01:37 AM 0
Share

what are you moving to? velocity towards mouse position? or add velocity when mouse moves? the rest of the script would be helpful

avatar image hexagonius · Feb 16, 2017 at 09:52 PM 0
Share

$$anonymous$$oving a scene object towards the mouse position mixes two different coordinate system. And even though it looks like the mouse moves over the exact spot you see in the scene it's still pixels vs. units. Try transfor$$anonymous$$g the mouse coordinate correctly into the scene.

 //$$anonymous$$y unique direction
  _direction = Camera.main.ScreenToWorldPoint(new Vector3(_mousePosX, _mousePosY,0)).normalized;
  
  //Want the item to follow exactly this direction line
  _item.GetComponent<Rigidbody2D>().velocity = _direction;

This method assumes your camera is orthographic. Otherwise the z value of the transformed Vector3 must be larger than 0.

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

95 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

Related Questions

how to clamp( set max value) the velocity of a 2d rigidbody? 1 Answer

Climbing code problem (2d physic). 0 Answers

Rigidbody Velocity in negative direction 0 Answers

Adding velocity to an object in a brick breaker game 0 Answers

Trying to add force to an object... is this done correctly? 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