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 AlycePants · Jun 19, 2014 at 02:12 PM · c#physicsphysics2dmaths

Why does this cause velocity to rotate with the rigidbody2D?

I'm new to the whole game development thing here, and maybe I'm just being super dumb, but can someone please tell me why the following code allows the player to steer even when not applying force?

 void FixedUpdate () {
     rigidbody2D.AddRelativeForce (DriveSpeed * Vector2.up * Input.GetAxis ("Vertical"));
     rigidbody2D.AddTorque (TurnSpeed * -Input.GetAxis ("Horizontal"));
     var locVel = transform.InverseTransformDirection (rigidbody2D.velocity);
     locVel.x = 0f;
     rigidbody2D.velocity = transform.TransformDirection (locVel);
 }

The intended outcome was to limit movement to only a single local axis (forwards, backwards), but instead I appear to have accidentally created some form of car. While I am interested in the best way to achieve this, I'm mainly trying to understand what I have actually done here.

Thanks for your help :)

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 Jeff-Kesselman · Jun 19, 2014 at 02:13 PM 0
Share

Are you still adding toque and is your object against another object, like the floor?

Think of a car wheel. All it has is torque but it makes you move because of friction with the road...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TKS_Keeper · Jun 19, 2014 at 03:52 PM

To answer this we are going to break this down line by line:


 /*
     This line will apply a force along the Vector2.up axis in the vehicles space,
     so if it has been turned it will go up relative to the vehicle and not
     relative to the screen.
     @see http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddRelativeForce.html
 */
 rigidbody2D.AddRelativeForce (DriveSpeed * Vector2.up * Input.GetAxis ("Vertical"));

 /*
     This line will add a rotational torque to the rigidbody, thus turning it 
     around its center of mass. If you wish to remove turning then remove this.
     @see http://docs.unity3d.com/ScriptReference/Rigidbody2D.AddTorque.html
 */
 rigidbody2D.AddTorque (TurnSpeed * -Input.GetAxis ("Horizontal"));

 /*
     This line retrieves the linear velocity in local space of the GameObject.
     @see http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html
 */
 var locVel = transform.InverseTransformDirection (rigidbody2D.velocity);

 /*
     This line sets the x value to 0, so removing the x axis value of the 
     velocity.
     @see http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html
 */
 locVel.x = 0f;

 /*
     This line transforms the new local velocity into world space velocity and 
     sets it directly. This is... not great, as it does cancle out any OTHER 
     external forces that may be acting on it and as the documentation states
     this kind of change is normally done via a call to AddForce.
     @see http://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html
 */
 rigidbody2D.velocity = transform.TransformDirection (locVel);



Remember than when you apply Torque you turn the object, and applying a relative force will push it along the axis relative to the vehicle, not the game world. This means that as you turn the vehicle the direction of the force turns as well. IF what you were looking for was to move the object on the vertical axis of world (as if there were a rail traveling up and down that it was traveling along for example) and then turn in place along that rail (as if it were a turret which was spinning on the carriage that traveled on the rails in the previous example) then what you want to use is Rigidbody2D.AddForce instead of Rigidbody2D.AddRelativeForce. This will make it move along the WORLDS Vector2.up instead of the RigidBody's Vector2.up.

Now, if you want to actually implement vehicle like behaviors I suggest looking into Unitysteer which is an open source project by Arges Systems. They handle quite a lot of physics based steering implementation, and it's a fairly nice system which nicely abstracts away common behaviors into a set of configurable behaviors.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Physics.Raycast to Physics2D.Raycast? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Help with 2D physics script 1 Answer

Reduce Physics2D Lag 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