• 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 xxluky · Oct 14, 2015 at 01:12 PM · transformvelocityconverting

How to convert Transform.position to Rigidbody2D.velocity?

Hi all good people,

I found out that if I am using transform.position of an object it ignores collisions. So the best way is to use rigidbody2D.velocity (it is a 2D game, using 2D colliders) but how to convert this line of code to get the exact same results?

 transform.position += transform.right * Time.deltaTime * planeSpeed;
Comment
Add comment · Show 1
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 maccabbe · Oct 14, 2015 at 01:25 PM -1
Share

Try using Rigidbody.$$anonymous$$ovePosition. i.e.

        Rigidbody  rigidbody=gameObject.GetComponent<Rigidbody>();
        rigidbody.$$anonymous$$ovePosition(transform.right * Time.deltaTime * planeSpeed);


http://docs.unity3d.com/ScriptReference/Rigidbody.$$anonymous$$ovePosition.html

3 Replies

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

Answer by DiegoSLTS · Oct 14, 2015 at 02:02 PM

I didn't notice you where using transform.right. You can do this instead:

 RigidBody2D body = GetComponent<RigidBody2D>();
 Vector2 velocityDirection = transform.TransformDirection(transform.right);
 velocity *= planeSpeed;
 body.velocity = velocity;
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 brazmogu · Oct 14, 2015 at 01:17 PM

You'll have to do a component sum for this, I guess, bcause as far as I know, you can't directly set the x and y components of RigidBody2D.velocity independently. Something like:

 RigidBody2D body = GetComponent<RigidBody2D>();
 body.velocity = Vector2.right * planeSpeed + Vector2.up * body.velocity.y;
Comment
Add comment · Show 4 · 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 DiegoSLTS · Oct 14, 2015 at 01:25 PM 1
Share

You can change the x and y value of a Vector2, what you can't do is change one velocity component directly to the "velocity" property. The reason is that body.velocity returns a copy of the velocity that's used internally, Rigidbody2D.velocity is a property, the actual Vector2 used to store the velocity is private.

You could do this in your case:

 RigidBody2D body = GetComponent<RigidBody2D>();
 body.velocity = new Vector2(planeSpeed, body.velocity.y);

Or also this:

 RigidBody2D body = GetComponent<RigidBody2D>();
 Vector2 velocity = body.velocity;
 velocity.x = planeSpeed;
 body.velocity = velocity;

But I don't think this is what the OP needs, I'll write another answer to explain why.

avatar image brazmogu DiegoSLTS · Oct 14, 2015 at 02:24 PM 0
Share

Ah, yes, I completely glossed over the transform.right there(which can be the same as Vector2.right, but not quite).

But, really, sum$$anonymous$$g the up and right vector components is just a fancy way of creating a vector without explicitly calling the constructor :P

avatar image DiegoSLTS brazmogu · Oct 14, 2015 at 02:35 PM 0
Share

I know you get the same result, but why do 2 multiplications and one sum when you can just create a new object (your approach will create a lot of temporary Vector2's)? It's an overcomplicated and confusing way to do that in this case.

Anyway, I just wanted to note that you CAN change the x and y values of a vector2, the warning here is that velocity (and position, and rotation, and more) are properties, not simple variables, and that's the only reason why you can't write:

 body.velocity.x = planeSpeed;
avatar image xxluky · Oct 14, 2015 at 01:34 PM 0
Share

Thanks for your reply. $$anonymous$$y code is moving a 2D sprite of a airplane. I am also changing transform.rotation of Z axes. When I change the rotation the result with my code is that the airplane flyes in that direction.

When I use your code, collision is now working properly but the airplane is not flying in other direction than to the right...

avatar image
0

Answer by DiegoSLTS · Oct 14, 2015 at 01:37 PM

You can't convert position to velocity, you're talking about values that mean completely different things and are expressed in diferent units.

If you want your rigidbody to move until it reachs the target position, set the velocity in that direction, and after some time the object will reach the position. You can check when it's "close enough" to the target position to set the velocity to back to zero (or check any other condition, like releasing a key).

To set the velocity you need something like:

 RigidBody2D body = GetComponent<RigidBody2D>();
 body.velocity = new Vector2(planeSpeed,0f);

Note that other physic interactions will reduce the velocity, so you'll have to keep setting it on every frame if you want a constant speed.

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 xxluky · Oct 14, 2015 at 01:46 PM 0
Share

Thanks for the explanation. But the airplane flies only to the right. It is not following the angle of a rotation the airplane is in. When I change the rotation I need the plane to fly in that angel... my code with transform.position is a good example. Please see THIS thread.

avatar image xxluky · Oct 14, 2015 at 02:13 PM 0
Share

Great! It is working better than expected! The rigidbody has a inertial effect that gives the plane a realistic behavior. Can you write it as an answer so I can accept it? Please check your spelling first: velocityDirection ins$$anonymous$$d of just velocity in the last two lines.

avatar image DiegoSLTS xxluky · Oct 14, 2015 at 02:36 PM 0
Share

Done, glad it worked better than expected!

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

31 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

Related Questions

Help around an approach to a "Virtual Motion Table" in Unity 0 Answers

How can a object be prevented from falling ? 1 Answer

Smoothly move continuous moving object 1 Answer

Getting a point base on move direction 2 Answers

How do I create a rubber band effect on a camera forward movement? 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