Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Add rotation value of one object to the current rotation value of another

Discussion in 'Scripting' started by crawl55, Feb 13, 2016.

  1. crawl55

    crawl55

    Joined:
    Jan 17, 2014
    Posts:
    8
    I am having problems adding the rotation value of one object to the "current" rotation value of the object that I want to move, so that the sum of the 2 angles is the angle the object is set to.

    What happens is when I call my function, the rotation goes until infinity , meaning it rotates like a clock, when I want it to rotate to the value and stop.

    Object a = 5°
    Object b (the object I want to rotate) = 45°
    sum = 50°.

    what I get is 50 then 55 then 60 then 65...so on

    code is below.

    Code (CSharp):
    1.  
    2.     void Torque ()
    3.     {
    4.         colAngle = -HelicopterControllerScript.hColl;
    5.         TorqueNeedlePreviousAngle = TorqueNeedle.transform.eulerAngles;
    6.         TorqueNeedle.transform.localRotation = Quaternion.Euler(0, 180, TorqueNeedlePreviousAngle.z);
    7.         float newangle = TorqueNeedlePreviousAngle.z + colAngle;
    8.         TorqueNeedle.transform.eulerAngles = new Vector3 (0, 180,newangle);
    9.     }
    10.  
    Any assistance is much appreciated.
     
  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    823
    You should read and set the localeulerangles in line 5 and 8. Other than that, maybe it is easier to work with Quaternions like that:
    Code (CSharp):
    1. colAngle = -HelicopterControllerScript.hColl;
    2. TorqueNeedle.transform.localRotation*= Quaternion.Euler(0, 0, colAngle);