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 empire2202 · Jul 01, 2013 at 04:14 PM · gameobjectrotatetimefaceanother

How to rotate a gameobject over time to face another

here's my code at the moment:

 function lookAt (){
     var rotation = Quaternion.LookRotation(target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 }

With this code my enemy space ship turns to face me (target variable), but it does this almost instantly, which isn't really very good considering it's turning a whole spacecraft. I need a way to make this rotation happen over time, this amount of time should be able to be altered by a variable rotateSpeed. I understand why this happens instantly, because I'm setting the objects rotation to face the object, not altering it over time every frame, but I can't think of how to do it. I know Javascript but I'm new to Unity.

Comment
Add comment · Show 7
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 deltamish · Jul 01, 2013 at 04:18 PM 0
Share

hi what is the value of damping

set it somewhere to 1.08f

avatar image robertbu · Jul 01, 2013 at 04:19 PM 0
Share

You code here looks fine. Quaternion.Slerp() does rotate over time. This use will result in an eased movement at the end of the rotation. Change 'damping' to rotateSpeed and set appropriately to the speed you want to rotate. If you don't want an eased movement, use Quaternion.RotateTowards() in place of Slerp().

avatar image robertbu · Jul 01, 2013 at 05:04 PM 0
Share

I assume you are calling lookAt() each frame.

avatar image empire2202 · Jul 03, 2013 at 10:23 AM 0
Share

yes, lookAt() is called every frame, but I'm gonna try out Travis' code then get back to you, but what I did was i played my project and moved the player with the scene editor and the enemy turned to face the player extremely quickly,shouldI just divide the Time.deltaTime by damping ins$$anonymous$$d of multiplying?

avatar image Travis-Bulford · Jul 03, 2013 at 01:45 PM 0
Share

Time.deltaTime is how much of a second has passed since the last frame to get smooth results you should always use that. So if you want the look at to take 5 seconds its Time.deltaTime/5

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Travis-Bulford · Jul 01, 2013 at 04:23 PM

You need to break your code into two parts.

The part that sets the direction you want to face and the part that turns towards that direction over time, and lets assume that the target object might move while we trying to look at it. Sorry code in c# should be easy enough to translate to js

 GameObject lookGameObject=null;
 
 // Set target
 public void lookAt (GameObject target){
     lookGameObject=target;
 }
 
 public void Update(){
     // Various update code
 
     if (lookGameObject!=null){
         lookAtRotation = Quaternion.LookRotation(lookGameObject.position - transform.position);
         // Will assume you mean to divide by damping meanings it will take damping seconds to face target assuming it doesn't move
         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime/damping);
 
         // Add code here to set lookGameObject to null if facing angle is close enough to our goal.
     }
 }

Afraid this was edited here could be code issues

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 empire2202 · Jul 03, 2013 at 03:25 PM 0
Share

thx for code, please refer to my comment on making the time not fixed but depend on how much the object rotates, is there a way to make the time it takes to complete the turn based on the amount the object has to turn ins$$anonymous$$d of fixed as the damping variable

avatar image Travis-Bulford · Jul 03, 2013 at 03:31 PM 0
Share

You might have missed my above comment.

This might be what you need

Ok so what you want is a speed that it will rotate at over time until it faces the direction you want not a time based rotation.

Try looking at

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html

It allows all the same properties but includes a speed factor.

avatar image empire2202 · Jul 06, 2013 at 09:07 AM 0
Share

Ok, could you please write a small piece of sample code showing me how to use SmoothDamp in this situation. Preferably using the variable rotation from the code in the OP and the variable damping to deter$$anonymous$$e speed, looking at the page you directed me to I can't quite understand how, do I just use the same code as in the OP but use SmoothDamp ins$$anonymous$$d of Quaternion.Slerp? I don't quite understand. Thanks

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

17 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

Related Questions

Rotate GameObject Issue 1 Answer

Rotate 90 over time on mouseDown 2 Answers

Trying to delay movement of a GameObject 1 Answer

How do I follow a gameobject with the camera 3 Answers

Rotating a turret body and head 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