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 serialdrifter · Feb 26, 2012 at 11:21 PM · rigidbodysmoothtranslatesteering

Translating a rigidbody smoothly

Hello,

I have a car, which is a rigidbody, and it's moving on the Z axis only. I wish to change lanes on the press of a button. So for example; the car is now driving on x:220, when i press a button, the car moves to x:215. Now, moving the car to that position is not really a problem, but i want it to look smooth, so it kinda steers towards it, and doesn't lose speed or anything, and it will stay on x:215 until i switch again. I can't find anything about translating rigidbodies smoothly, except the lerp function, which does not quite do what i want.

Can anyone point me in the right direction? Any help is appreciated!

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
2
Best Answer

Answer by aldonaletto · Feb 27, 2012 at 12:34 AM

Doing this with forces is very complex - you would have to write a PID controller, and it's not easy to adjust its parameters. A much easier way to do that is using Mathf.MoveTowards:

var sideSpeed: float = 2.5; var xPosition: float;

function Start(){ xPosition = transform.position.x; }

function FixedUpdate(){ var pos = rigidbody.position; pos.x = Mathf.MoveTowards(pos.x, xPosition, sideSpeed * Time.deltaTime); rigidbody.position = pos; } This will smoothly move the rigidbody in the X axis only to the desired xPosition. To change the car to a lane 5 meters to the right, for instance, just do this:

   xPosition -= 5;

and the car will move at sideSpeed meters per second to the new coordinate.
If you want a smoother change, use Lerp instead:

   pos.x = Mathf.Lerp(pos.x, xPosition, sideSpeed * Time.deltaTime);

The car will not move at constant speed, but in a constant time: it will start moving fast, and reduce the lateral speed slowly when reaching the desired position, taking the same time no matter how far the new lane is. Just to give you an idea, sideSpeed = 5 will move the car to the new lane in about 1 second.
NOTE: The car will not steer to the new lane; you can fake this steering by changing the car rotation based on the distance pos.x->xPosition:

   ...
   pos.x = Mathf.MoveTowards(pos.x, xPosition, sideSpeed * Time.deltaTime);
   rigidbody.position = pos;
   // adjust this number to get the desired angle:
   var angle = (xPosition - pos.x) * 2.3; 
   transform.eulerAngles = Vector3(0, angle, 0);
   ...




Comment
Add comment · Show 6 · 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 serialdrifter · Feb 27, 2012 at 12:57 AM 0
Share

Thanks for the answer, this works pretty well, except for the fact that my car is locked to 1 driving line, so this code still can't move the car. If i remove the axis lock, it works like a charm, but then a new problem arises, the car won't continue on a straight line.

This is the code i use for the axis lock, do i have to make it 4 times, for each of the lanes, or is there a easier/cleaner way? It would be best if i could just use A or Z and the car will move 5 meters left or 5 meters down, and then stay locked to that line.

if(transform.localPosition.x != 220){ transform.localPosition.x = 220;}

avatar image serialdrifter · Feb 27, 2012 at 05:07 PM 0
Share

Anyone? Still having problems

avatar image aldonaletto · Feb 28, 2012 at 01:12 AM 0
Share

You can remove your position lock code and let physics do the job for you: set Freeze Position X and Y under Rigidbody/Constraints in the Inspector (set Freeze Rotation X Y and Z too) - physics will only move the rigidbody in the Z axis, but you can move it with the script above. This way you can move the car using the A and Z keys as you want:

var curLane = 0; var maxLanes = 4;

function Update(){ if (curLane > 0 && Input.Get$$anonymous$$eyDown("a")){ xPosition -= 5.0; curLane--; } if (curLane < maxLanes && Input.Get$$anonymous$$eyDown("z")){ xPosition += 5.0; curLane++; } }

avatar image serialdrifter · Feb 28, 2012 at 03:17 AM 0
Share

It works now, thank you very much for the explanation. :)

avatar image AdroitViper · Feb 26, 2014 at 04:33 PM 0
Share

which method finally solved this for you? I have pretty much exactly the same problem

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Airplane smooth acceleration problem 0 Answers

Moving objects with attached colliders not colliding? 1 Answer

What's the best move/rotate method to simulate a herd of animals moving around? 2 Answers

Rigidbody Movement 1 Answer

Aligning a rigidbody smoothly with the perpendicular of a vector angle 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