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 /
  • Help Room /
avatar image
0
Question by NootVince · Nov 29, 2021 at 05:31 PM · quaternion.lookrotationquaternion.slerp

I can't get my Quaternion rotation to work

The first line of my code below makes my object follow the rotation of the object it collides with. The part after that is supposed to make the object rotate 180 degrees (go in opposite direction of collision object), but doesn't do anything at all. I've tried a few different approaches, but none of them seem to do anything.

Any help is much appreciated!

 // follow the rotation of the object it collides with:
 gameObject.transform.rotation = other.transform.rotation;


 // rotate 180 degrees (go in opposite direction of collision object):
 var lookPos = other.transform.position - transform.position;
 lookPos.y = 0;
 var rotation = Quaternion.LookRotation(lookPos);
 rotation *= Quaternion.Euler(0, 90, 0); // this adds a 90 degrees Y rotation


 // apply interpolation over time:
 transform.rotation = Quaternion.Slerp( transform.rotation , rotation , Time.deltaTime*smooth );
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 andrew-lukasik · Nov 29, 2021 at 03:52 PM 0
Share

Slerp makes sense when called in any of the Update methods. But guessing from "(...) object it collides with (...)" you nested this code inside OnColliderEnter or OnColliderStay method , which won't be called often enough for interpolation to work properly or at all.

avatar image NootVince andrew-lukasik · Nov 29, 2021 at 04:25 PM 0
Share

Thanks so much for your reply. That makes perfect sense. So I move this code into the update function and then call that part of the code from the OnColliderEnter (?)

avatar image andrew-lukasik · Nov 29, 2021 at 05:17 PM 0
Share

option 1: animate in Coroutines

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class InterpolateRotationExample : MonoBehaviour
 {
     [SerializeField][Min(0.0001f)] float _interpolationDuration = 2f;
     Coroutine _animationCoroutine = null;
     
     void OnCollisionEnter ( Collision collision )
     {
         if( _animationCoroutine!=null ) StopCoroutine( _animationCoroutine );// prevents 2 rotation coroutines (animations by code) being executed at the same time
         Transform otherTransform = collision.transform;
         var startRot = transform.rotation;
         var targetRot = Quaternion.LookRotation( -otherTransform.forward , otherTransform.up );
         _animationCoroutine = StartCoroutine( InterpolateRotationOverTime(startRot,targetRot,_interpolationDuration) );
     }
 
     IEnumerator InterpolateRotationOverTime ( Quaternion src , Quaternion dst , float duration )
     {
         Debug.Log("started");
         float timeStart = Time.time;
         float completion = 0;
         var waitForFixedUpdate = new WaitForFixedUpdate();
         while( completion<1 )
         {
             yield return waitForFixedUpdate;// code execution stops and waits until FixedUpdate (physics update) basically, then continues
             completion = ( Time.time - timeStart ) / _interpolationDuration;
             transform.rotation = Quaternion.Slerp( src , dst , completion );
         }
         Debug.Log("\tdone :T");
     }
 
 }

option 2: animate in update methods

 using UnityEngine;
 
 public class InterpolateRotationExample : MonoBehaviour
 {
     [SerializeField][Min(0.0001f)] float _interpolationDuration = 2f;
     float _interpolationStart;
     Quaternion _quatSrc, _quatDst;
 
     void Start ()
     {
         enabled = false;
     }
 
     void OnCollisionEnter ( Collision collision )
     {
         Transform otherTransform = collision.transform;
         _quatSrc = transform.rotation;
         _quatDst = Quaternion.LookRotation( -otherTransform.forward , otherTransform.up );
         _interpolationStart = Time.time;
         enabled = true;
     }
 
     void FixedUpdate ()
     {
         float t = ( Time.time - _interpolationStart ) / _interpolationDuration;
         transform.rotation = Quaternion.Slerp( _quatSrc , _quatDst , t );
         if( t>=1 )
         {
             enabled = false;
         }
     }
 
 }
avatar image andrew-lukasik andrew-lukasik · Nov 29, 2021 at 05:33 PM 0
Share

Try this ^

avatar image NootVince andrew-lukasik · Nov 29, 2021 at 07:05 PM 1
Share

I really appreciate your help. I will have to try and implement this tomorrow. I'm pretty new to Unity so I will need some time to see if I can get this to work with my code. Thanks a lot!

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

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

Help with Quaternion.Slerp movement? 1 Answer

[SOLVED] How to LookRotation in only Y axis 0 Answers

Can't turn Y axis if terrain Hugging 0 Answers

Getting a smooth rotation with Quaterminions slerp in IEnumerator 1 Answer

Enemy rotation while looking. Help. 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