Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Can't change the Rotation.z value to a duplicate Object

Discussion in 'Scripting' started by Sersiego, Aug 19, 2023.

  1. Sersiego

    Sersiego

    Joined:
    Nov 17, 2015
    Posts:
    3
    Hello,
    Just started with Unity after I left Swift/xCode and I can't find a solution to my problem.
    I duplicated an object in the Scene and I positioned it with a new rotation value.

    Captura de pantalla 2023-08-19 a las 14.42.35.png

    But when I tried to spin this duplicate object it takes the rotation.z value of the original.
    If I make it rotate 30º it goes 30º from the position of the original object, never 30º from its position.

    In my script I tried to obtain the Rotation.z value of this duplicate object to then add the rotation value that I want.
    But this solutions didn't work at all.

    Code (CSharp):
    1. public class Vayven : MonoBehaviour
    2. {
    3.     [SerializeField] GameObject rayo2;
    4.     float r;
    5.     public float anguloIni;
    6.  
    7.     void Start()
    8.     {
    9.         anguloIni = rayo2.transform.rotation.z;
    10.     }
    11.  
    12.     void Update()
    13.     {
    14.         float Angle = Mathf.SmoothDampAngle(transform.eulerAngles.z, anguloIni + 30, ref r, 1.0f);
    15.         transform.rotation = Quaternion.Euler(0, 0, Angle);
    16.     }
    17. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    Sersiego likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,814
    ALSO... THIS:

    Is NOT an Euler angle. Don't use it. That value has zero meaning to you and I.
     
  4. Sersiego

    Sersiego

    Joined:
    Nov 17, 2015
    Posts:
    3
    Thanks Kurt-Dekker. I'm going to study more about C# and Unity. This is not that straightforward coming from swift as I thought
     
  5. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    435
    Im not sure if this helps you out, but a lot easier for my brain was to use the function "transform.Rotate" (there are few overrides). Most time I don't need to bother about angles, only game coordinates, vectors, and around what will be rotation.
    Code (CSharp):
    1. void Update()
    2. {
    3.     // Rotate character/vehicle left and right by player input
    4.     transform.Rotate(Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);
    5. }
    https://docs.unity3d.com/ScriptReference/Transform.Rotate.html