Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Apr 06, 2017 at 02:28 PM by specular12 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by specular12 · Apr 05, 2017 at 10:28 PM · rotationquaternionparentlookrotation

Quaternion.Slerp not working

Hi guys, In a game set in space I'm working on, I can't seem to get an object to face the camera, as well as end up level to the camera, using LookRotation(). I believe it's because the camera could be at any arbitrary angle. I have been able to get the rotation to work on two axis, x & y, but the object is not rotated correctly on the z axis so it's not level with respect to the camera.

Setup: An object is parented to an empty GameObject which in turn is parented to my camera. The empty GameObject is 15 units down the Z-axis, and the object is parented to the empty GameObject's origin.

The camera flies around space before stopping, and the object appears in front of the camera rotating using a rigid body . So when the tractor beam is activated by the user, the position / rotation of the camera is arbitrary and the Slerp rotates the object. The script below works fine until the camera is rotated.

using UnityEngine; using System.Collections;

public class test_rotate : MonoBehaviour {

 public Transform target;
 Vector3 targetV3;   // Vector3 of camera (for LookRotation)
 Quaternion rotateTo; //used to store LookAt angle
 bool beamOn; 
 public Rigidbody rb;
 int counter = 0; //counter for frames 
 float tracterBeamSpeed = 5f; //modifies the speed of rotating the object to face camera

 void Start () {
     rb.AddRelativeTorque (new Vector3 (50f, 500f, 150f));  //sets the object rotating
 }

 void Update () {
     if (beamOn) 
     {
         counter++;
         if (counter <= 40) {
         transform.rotation = Quaternion.Slerp (transform.rotation, rotateTo, Time.deltaTime * tracterBeamSpeed);
         Debug.Log("rotating" +counter);
         } else {
             beamOn = false;
             counter = 0;
             Debug.Log("done rotating");
         }
     }
 }
 //button triggers this method
 public void RotateStart()     
 {
         rb.freezeRotation = true; //turn of rigidbody forces for rotation
         //tried this, works if rotate camera x and / or y but not if rotate camera.z
         targetV3 = target.forward; 
         //******** tried this, doesn't work if camera has been rotated, and since object is        
                                      // parented relative position may not be necessary
         //targetV3 = (transform.position - target.position); 
         rotateTo = Quaternion.LookRotation(targetV3); 
         //******** also tried this, same result, doesn't work when camera is rotated
         //rotateTo = Quaternion.identity;
         beamOn = true;  //turns on the Slerp function in Update
 }

}

Thanks for any advice!

By the way, if I pause the game and type 0,0,0 as the eular angle for rotation in the the editor for the object, it faces the camera perfectly, which is why I tried using Quaternion.identity

Comment
Add comment · Show 1
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 hexagonius · Apr 06, 2017 at 05:53 AM 1
Share

a way world be to use:
Quaternion.LookRotation(camera.position - object.position, camera.up);

1 Reply

  • Sort: 
avatar image
0

Answer by specular12 · Apr 06, 2017 at 02:13 PM

Thanks Hex, Genius! Worked like a charm. Adding the .up parameter to LookRotaion() solved the problem.

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

Follow this Question

Answers Answers and Comments

113 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 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 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 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 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 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

How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer

Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers

how to rotate bullet towards enemydirection? 0 Answers

Problem with Quaternion and Euler Angles 0 Answers

Delayed LookAt, but only on one axis 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