Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

How to rotate an object over time using C#

Discussion in 'Scripting' started by taciturnsays, Jan 1, 2017.

  1. taciturnsays

    taciturnsays

    Joined:
    Mar 12, 2016
    Posts:
    18
    This script is to rotate an object on his local axis over time.
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class rotateLocalAxis : MonoBehaviour {
    7.     public Vector3 rotationDirection;
    8.     public float durationTime;
    9.     private float smooth;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.         smooth = Time.deltaTime * durationTime;
    19.         transform.Rotate(rotationDirection * smooth);
    20.     }
    21. }
    22.  
    To get him to rotate on his own axis every second i need to set the durationTime = 200,
    how do i fix this to 1 = 1 second?
     
    Last edited: Jan 1, 2017
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    You multiply it by 200
     
    Last edited: Jan 1, 2017
    taciturnsays likes this.
  3. taciturnsays

    taciturnsays

    Joined:
    Mar 12, 2016
    Posts:
    18
    lol now im having that dahh moment kkk thank you very much . Sometimes you just miss the obvious!

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class rotateLocalAxis : MonoBehaviour {
    7.    public Vector3 rotationDirection;
    8.    public float smoothTime;
    9.    private float convertedTime = 200;
    10.    private float smooth;
    11.  
    12.    // Use this for initialization
    13.    void Start () {
    14.    
    15.    }
    16.  
    17.    // Update is called once per frame
    18.    void Update () {
    19.      smooth = Time.deltaTime * smoothTime * convertedTime;
    20.      transform.Rotate(rotationDirection * smooth);
    21.    }
    22. }
    23.  
    Also i tried to use the Space.World for world rotation,
    transform.Rotate(rotationDirection * smooth, Space.World);
    but its doing the same as the local world instead of rotating around , am i missing something over here?
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,175
    Spaceworld is useful if you're rotating a child afaik
    If you want to rotate around a point, use RotateAround()
     
    taciturnsays likes this.
  5. taciturnsays

    taciturnsays

    Joined:
    Mar 12, 2016
    Posts:
    18
    How do i make the object with this script follow the height or y position of the orbitCenter position?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rotateWorldAxis : MonoBehaviour {
    6.     public Vector3 centerPoint; // manual world center position
    7.     public GameObject orbitCenter; // GameObject as origin;
    8.     private Vector3 orbitPosition;
    9.  
    10.     public Vector3 rotationDirection;
    11.     private float convertedTime = 200;
    12.     public float smoothTime;
    13.     private float smooth;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.  
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update () {
    22.         orbitPosition = orbitCenter.transform.position;
    23.  
    24.         smooth = Time.deltaTime * smoothTime * convertedTime;
    25.         transform.RotateAround(orbitPosition, rotationDirection, smooth); // use orbitPosition replaced with centerPoint as needed
    26.     }
    27. }
    28.  
     
    Last edited: Jan 3, 2017
unityunity