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 Jason-H · Mar 31, 2011 at 03:17 PM · rotationrigidbodyrotate

Rotating a rigid body to face left or right smoothly

Hi, I'm trying to get my rigid body character to face either left or right but with a smooth movement. I can make it turn instantly with the following code;

if (rigidbody.velocity.x>0.05){
        transform.rotation.y=0;
}
if (rigidbody.velocity.x<-0.05){
        transform.rotation.y=180;
}

But how can I make it so that it rotates around the y axis over the course of about half a second, instead of instantly snapping to either 0 or 180 degrees?

I have tried using Quaternion.Slerp but wasn't too sure what I was doing with it and not even sure if that's the right method.

Oh and in case it makes a difference, the rigid body character that I want to rotate is fixed on the z plane.

Any help is much appreciated! 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

4 Replies

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

Answer by Meltdown · Mar 31, 2011 at 04:03 PM

You can use a combination of Quarternion.LookRotation and Slerp to rotate your object to a certain point in space over time...

Something like this...

public GameObject targetPosition;

 void Update () {
     var targetPoint = targetPosition.transform.position;
     var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f);
 }

Where targetPosition is the point in space you want to turn your object to.

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 Jason-H · Mar 31, 2011 at 06:11 PM 0
Share

Thanks a lot, I got it working! Very much appreciated!

avatar image Jason-H · Apr 06, 2011 at 07:47 PM 0
Share

Is the best way to do this by using two empty game objects at positions (1000,0,0) and (-1000,0,0) that the character looks at or can it be made to slowly rotate the character to 180 degrees ins$$anonymous$$d of facing a point way of in the distance?

avatar image
1

Answer by Jesse Anders · Mar 31, 2011 at 04:27 PM

Note also that you're treating Transform.rotation as a set of angles, but it's actually a quaternion. Because of the way Unity handles things internally, assigning 180 to rotation.y coincidentally has the effect of a 180-degree rotation about the y axis. However, this is pure happenstance; the elements of a quaternion are not angles, and assigning angles to them will produce more or less random results (although as you've found, the results may accidentally turn out to be correct in some cases :)

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
avatar image Jason-H · Mar 31, 2011 at 06:11 PM 0
Share

Ahh yes, I noticed that after playing with this. Thanks for the heads up

avatar image
0

Answer by Joshua · Mar 31, 2011 at 03:59 PM

Use this. For all 'smoothed' things, check out lerping.

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 saadali211 · Feb 21, 2017 at 10:43 PM

https://www.youtube.com/watch?v=Csi3LHyQSAc

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

1 Person is following this question.

avatar image

Related Questions

how to tilt boat on turning 2 Answers

How to turn a car like in real world? 1 Answer

add force to rotation 1 Answer

Player character X rotation going haywire on game start 1 Answer

How to make the character move and rotate where the camera is facing (3rd person) 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