Search Unity

How to rotate only the ZAxis of object?

Discussion in 'Scripting' started by xXAl-HarereXx, Feb 16, 2020.

  1. xXAl-HarereXx

    xXAl-HarereXx

    Joined:
    Aug 21, 2017
    Posts:
    101
    I have been trying to rotate only the z axis constantly while he false but I cant seem to figure it out.
    How can I make the object rotate constantly on the z axis only? Using an animation clip resets the position while I want the object to rotate starting from its current z position.

    Hope someone helps :)
     
  2. Tom-Atom

    Tom-Atom

    Joined:
    Jun 29, 2014
    Posts:
    153
    If you just want to rotate object forever, then add this component to game object:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Rotate : MonoBehaviour {
    4.  
    5.     [SerializeField] private float _anglesPerSecond = 90;
    6.  
    7.     // ------------------------------------------------------
    8.     void Update() {
    9.  
    10.         Vector3 rotation = transform.localEulerAngles;
    11.         rotation.z += Time.deltaTime * _anglesPerSecond;
    12.         transform.localEulerAngles = rotation;
    13.     }
    14. }
    15.  
    You can also examine for Vector3.RotateTowards() - https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html. It should have all you need, if your needs are more complex (like rotating towards some target, etc.)
     
  3. xXAl-HarereXx

    xXAl-HarereXx

    Joined:
    Aug 21, 2017
    Posts:
    101

    THANK YOUUUUUUU
     
  4. Bear_Roar_Software_Development

    Bear_Roar_Software_Development

    Joined:
    Mar 10, 2022
    Posts:
    1
    Worked for me to. Thanks!!
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Please note you can just click the "Like" button, no need to necro a thread.

    I'll move this to the Scripting forum where it belongs.
     
    Deleted User and Bunny83 like this.
  6. Pixelated_Worlds

    Pixelated_Worlds

    Joined:
    Feb 28, 2024
    Posts:
    1
    None of these responses help.
     
    YaraJ likes this.