• 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 bAStek · Mar 05, 2014 at 02:55 PM · rotationrotation axis

How to calculate direction of rotation/start rotating object towards another object but not finish when object is in front of another object

I've got 2 objects in my Scene. Basically i want to rotate object_1 towards object_2 but first i want object_1 to rotate few times around and then to face object_2 so the Slerp/Lerp/RotateTowards won't help me. I am using:

Quaternion.LookRotation((object_2.transform.position - object_1.transform.position).normalized);

as my final rotation and

object_1.transform.rotation

as my starting rotation.

I want to start rotating object_1 towards object_2 but i want that object_1 don't stop when facing object_2 but keeps rotating in same direction as when start. Any idea how can I calculate angles/axises to rotate my object in this way? Or is there a way to calculate angle value to rotate by some axis using Quaternion.ToAngleAxis() function?

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

Answer by robertbu · Mar 05, 2014 at 03:22 PM

This question can be easy or hard. It depends on the rotations. If you are only rotating around the 'y' axis, then it is easy. You can calculate a LookRotation() and just use the eulerAngles.y to get the rotation. If the rotation is more arbitrary then it might be more difficult. It depends on your angles and constraints.

Here is a bit of code that implements the rotation around the 'y' axis.

 #pragma strict
 
 var target : Transform;
 
 function Update() {
     if (Input.GetMouseButtonDown(0)) {
         SpinAndLook(target, true, 2.0);
     }
 }
 
 function SpinAndLook(target : Transform, direction : boolean, time : float) {
     var q = Quaternion.LookRotation(target.position - transform.position);
     var endAngle = q.eulerAngles.y;
     var startAngle = transform.eulerAngles.y;
     
     if (direction) 
         endAngle += 720.0;
     else
         endAngle -= 720.0;
         
     var timer = 0.0;
     
     while (timer < time) {
         var angle = Mathf.Lerp(startAngle, endAngle, timer / time);
         transform.eulerAngles = Vector3(0,angle,0);
         yield;
         timer += Time.deltaTime;
     }
 }
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 bAStek · Mar 06, 2014 at 07:20 AM 0
Share

Well, unfortunately i need to rotate around all 3 axises - that's the main problem. :(

avatar image robertbu · Mar 06, 2014 at 07:44 AM 0
Share

So here is some code that will do as you ask, but this code picks the axis of rotation:

 #pragma strict
 
 var target : Transform;
 
 function Update() {
     if (Input.Get$$anonymous$$ouseButtonDown(0)) {
         SpinAndLook(target, true, 2.0);
     }
 }
 
 function SpinAndLook(target : Transform, direction : boolean, time : float) {
     var q = Quaternion.FromToRotation(transform.forward, target.position - transform.position);
     
     var angle = 0.0;
     var axis = Vector3.zero;
     q.ToAxisAngle(axis, angle);
     angle *= $$anonymous$$athf.Rad2Deg;
     
     var endAngle = angle + 720;
     var startRotation = transform.rotation;
     
     var timer = 0.0;
     
     while (timer < time) {
         var a = $$anonymous$$athf.Lerp(0, endAngle, timer / time);
         transform.rotation = Quaternion.AngleAxis(a, axis) * startRotation;
         yield;
         timer += Time.deltaTime;
     }
     transform.rotation = q * startRotation;
 }
avatar image bAStek · Mar 06, 2014 at 08:41 AM 0
Share

It works great. Thank you very much! I tried something like this before but i didn't use $$anonymous$$athf.Rad2Def on angle. Once again, 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image

Related Questions

Problem when i rotate a 2d object aroun Z-axis 1 Answer

How do you make a Tps cam with RotateAround around the X and Y axis without rotating on the Z axis? 1 Answer

How can I rotate a vector direction around an arbitrary axis? 0 Answers

how can i make an object rotate on the scenes axis 0 Answers

How to change rotation of a child GameObject around its own axis instead of that of the Parent? 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