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 /
avatar image
0
Question by deeppratim123 · Mar 20, 2019 at 07:17 AM · rotate objectquaternion.slerp

how to rotate a gameobject back and forth smoothly ,How to rotate a gameObject back and forth smoothly with a specified time interval.

Hello. So I am have a gameobject that needs to rotate backwards smoothly wait for a few seconds and then rotate back to its original position again smoothly.Here is my code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class LookBehind : MonoBehaviour {

 public float turnRate = 4f;
 public float waitTime;


 private float timeSinceLastTurned;

 private Quaternion originalRotation;
   

 private float rotationSpeed = 10.0f;
  
  // Start is called before the first frame update
 void Start()
 {
     originalRotation = transform.rotation;
 }

 // Update is called once per frame
 void Update()
 {
     timeSinceLastTurned += Time.deltaTime;

    
     if (timeSinceLastTurned >= turnRate) 
     {
         
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(0, -180, 0), Time.deltaTime * rotationSpeed);
        
         waitTime += Time.deltaTime;
         if ( waitTime >= turnRate ) 
         {
             transform.rotation = Quaternion.Slerp(transform.rotation,originalRotation, Time.deltaTime * rotationSpeed);
             
             waitTime = 0;
             timeSinceLastTurned = 0;
             
         }
         
      }

     
   }

}

Now the problem is that the gameobject rotates smoothly backwards using Quaternion.Slerp but it shows irregular rotation when it needs to rotate back to its original position.I think the second Quaternion.Slerp is causing this problem. Could you help me find a solution to this problem by making changes in the same code?

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

Answer by xxmariofer · Mar 20, 2019 at 09:15 AM

slerp should not be used like that, you can change Slerp for RotateTowards, and should work, you need to use Slerp iun particular for any reason? since its a bit harder to explain, slerp interpolates between 2 positions, so for linear interpolating between 2 objects you should not used the transform.position as the first position.

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

101 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

Related Questions

How can you make a 2D object move based on rotation, and rotate while moving? 1 Answer

How can i rotate only Z axis of Player. 1 Answer

How can I make the gun rotate and shoot like lost control and shooting all around ? 0 Answers

Rotate 3D object relative to the camera 0 Answers

Why do VFX particles change velocity depending on the systems rotation when simulating in world space? 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