Search Unity

Rotate object back and forth x degrees

Discussion in 'Scripting' started by ManuelCosta, Dec 24, 2015.

  1. ManuelCosta

    ManuelCosta

    Joined:
    Dec 24, 2015
    Posts:
    9
    I have an object as child of another that casts and draw rays in front of it in a chape of a cone (2D)
    I want to rotate the children (vision cone) 45 degrees to the left and right to represent his vision.

    The thing is....when rotating counter clockwise, the numbers increase making it impossible to control the rotation.

    For better understanding i will post my code:
    Code (csharp):
    1.  
    2. public void RotateVision()
    3.   {
    4.   if (rotateClockWise)
    5.   {
    6.   transform.Rotate(new Vector3(0, rotationAmount, 0));
    7.   if (transform.rotation.eulerAngles.y >= 45)
    8.   {
    9.   rotateClockWise = false;
    10.   }
    11.   }
    12.   else
    13.   {
    14.   transform.Rotate(new Vector3(0, -rotationAmount, 0));
    15.   if (transform.rotation.eulerAngles.y <= 315)
    16.   {
    17.   rotateClockWise = true;
    18.   }
    19.   }
    20.   }
    21.  
    22.  
     
    Last edited: Dec 24, 2015
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    First, refer to this post, it makes your code more readable:
    http://forum.unity3d.com/threads/using-code-tags-properly.143875/

    As for the answer, instead of allow the quaternion in the transform to store the state of rotation (which can come back with wildly different values as it loops around 360 degrees). Instead have a field/variable in your script that maintains the state, and you just update your rotation value with that.
     
  3. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Try this.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Rotate : MonoBehaviour {
    5.     [SerializeField] protected Vector3 m_from = new Vector3(0.0F, 45.0F, 0.0F);
    6.     [SerializeField] protected Vector3 m_to = new Vector3(0.0F, -45.0F, 0.0F);
    7.     [SerializeField] protected float m_frequency = 1.0F;
    8.  
    9.     protected virtual void Update() {
    10.         Quaternion from = Quaternion.Euler(this.m_from);
    11.         Quaternion to = Quaternion.Euler(this.m_to);
    12.  
    13.         float lerp = 0.5F * (1.0F + Mathf.Sin(Mathf.PI * Time.realtimeSinceStartup * this.m_frequency));
    14.         this.transform.localRotation = Quaternion.Lerp(from, to, lerp);
    15.     }
    16. }
     
  4. ManuelCosta

    ManuelCosta

    Joined:
    Dec 24, 2015
    Posts:
    9
    Thank you. It works!
     
    Polymorphik likes this.
  5. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Glad I could help
     
  6. irfanbaysal1997

    irfanbaysal1997

    Joined:
    Sep 17, 2018
    Posts:
    6

    can you explain the formula that you used after float lerp equations ı mean like this part float lerp = 0.5F * (1.0F + Mathf.Sin(Mathf.PI * Time.realtimeSinceStartup * this.m_frequency));
     
  7. JordanSchuetz

    JordanSchuetz

    Joined:
    Jan 23, 2013
    Posts:
    7
    Thanks Polymorphik works great!
     
  8. Sinim

    Sinim

    Joined:
    Aug 9, 2018
    Posts:
    1
    Thanks Guys works perfectly!
     
  9. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    Currently this rotates, stops and rotates back.
    Can you please help provide the solution for rotate, stop (x time) rotate back?