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 Sajjoo · Oct 06, 2011 at 06:23 AM · rotationcarunityiphone

how to rotate a car smoothly?

Hello guys, i am working on Car AI and i want my cars to change the lanes aswell. everything is going right except the rotation of car, it is not smooth.i also have to set the rotation to 0 when the car has changed its lane.

i am doing like this.

 transform.Rotate(0, rotationValue, 0); // rotationValue = -5 or 5 

and when it has changed the lane i set the rotationValue to 0. Any help or hint will be appreciative

thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Oct 06, 2011 at 02:05 PM

Rotate should be used only for continuous rotations, because it can accumulate errors and let your model tilted to some weird angle after some time. For tilting and other small angle changes, it's always better to set transform.localEulerAngles.
The function below use this property to turn the car like you said: turns smoothly to the angle during half the time, then turns back to zero in the other half. Additionally, it "banks" to the side automatically, like real cars do when changing lanes - if you don't need this, replace bank by zero when setting the localEulerAngles.
You may place this code in the car script or in any other object, because you must pass the car transform when calling the function. Since it is a coroutine, you just call it when starting changing lanes, and the routine does the rest automatically.
I included a test Update function that I used to fine tune the routine - attach this script to the car and use the keys A or D to change to the right or left lane.

private var turn: float = 0; private var changing = false;

function ChangeLane(car:Transform, angle: float, time: float){ var t: float; var bank: float; if (changing) return; changing = true; for (t = 0; t < 1;){ t += 2*Time.deltaTime/time; turn = Mathf.Lerp(turn, angle, t); bank = 0.5 turn; car.localEulerAngles = Vector3(0, turn, bank); yield; } for (t = 0; t < 1;){ t += 2*Time.deltaTime/time; turn = Mathf.Lerp(turn, 0, t); bank = 0.5 turn; car.localEulerAngles = Vector3(0, turn, bank); yield; } changing = false; }

// This is just for testing purposes - you must call ChangeLane in your script // when changing lanes. Attach this script to the car to test it.

function Update(){ if (Input.GetKeyDown("a")){ ChangeLane(transform, 5, 1); } if (Input.GetKeyDown("d")){ ChangeLane(transform, -5, 1); } }

Comment
Add comment · Show 2 · 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 Sajjoo · Oct 06, 2011 at 04:04 PM 0
Share

Your solution is good but i have also applied rigidbody on my car and it does not move my car smoothly and giving me some weird effects aswell.

avatar image aldonaletto · Oct 07, 2011 at 03:36 AM 0
Share

You can use a trick: have a main object with the rigidbody, and child your car model to it. When changing lanes, pass the car model transform to ChangeLane - only the model will turn and bank, and the main object with the rigidbody will not be affected.

avatar image
1

Answer by malik.tariq49 · Jan 20, 2015 at 11:44 AM

Try this code if (Input.GetKey (KeyCode.LeftArrow)) {

transform.Rotate (new Vector3 (0f, -1f, 0f)); } if you go for right arrow then value of y should be plus just like that (0f,1f,0f )

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help with car rotaton 0 Answers

2d detect 360 (backflip) rotation on z axis? 2 Answers

Camera position/rotation reset after 5 seconds of mouse inactivity 0 Answers

Ignoring force of impact for rotation axis only 1 Answer

Tracking Rotation for Flips/rolls 3 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