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. Dismiss Notice

Question Local rotate and stop at new position?

Discussion in 'Scripting' started by Vaupell, Jan 25, 2021.

  1. Vaupell

    Vaupell

    Joined:
    Dec 2, 2013
    Posts:
    301
    Hi

    Playing with rotating blades on a wind turbine..
    upload_2021-1-25_9-10-12.png

    I am trying to pitch the blades to a angle,
    Initially i was using
    Code (csharp):
    1. BladeC.GetComponent<Transform>().localRotation = Quaternion.Slerp(Quaternion.Euler(240, 0, 0), Quaternion.Euler(0, NewAngle, 0), 0.1f * Time.deltaTime);
    But that is not a "local" transform, and caused rotation on unwanted sides..

    I only wish to rotate around it self, and i do that with

    NewAngle is a int giving the new pitch angle.

    Code (csharp):
    1.  
    2.  
    3.         BladeA.GetComponent<Transform>().localEulerAngles += new Vector3(0f, NewAngle, 0f) * 0.5f * Time.deltaTime;
    4.         BladeB.GetComponent<Transform>().localEulerAngles += new Vector3(0f, NewAngle, 0f) * 0.5f * Time.deltaTime;
    5.         BladeC.GetComponent<Transform>().localEulerAngles += new Vector3(0f, NewAngle, 0f) * 0.5f * Time.deltaTime;
    6.  
    However, ofcourse it keeps spinning around itself, but atleast on the correct axis. :D

    Any suggestions on how i can achieve this, and make it stop at it's desired angle?
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    First you shouldn't use GetComponent in Update. It's eating up too much resources and bad practice. Get it once and store it.
    About your Question: You should have a look at Lerp.
    My Approach would be somethin like this:
    Code (CSharp):
    1.  
    2. float elapsedTime = 0f;
    3. float startVal = 0f;
    4. float goalVal = 10f;
    5.  
    6. void Update()
    7. {
    8.  if(elapsedTime < 1f)
    9.  {
    10.    elapsedTime += Time.deltaTime;
    11.    elapsedTime = Mathf.Clamp(elapsedTime, 0f, 1f);//not necessarily needed, but hey...
    12.    myCachedTransform.localEulerAngles = new Vector3(0f, Mathf.Lerp(startVal, goalVal, elapsedTime), 0f);
    13.  }
    14. }
    15.  
    16. //This might be a neat extension..
    17. void SetNewRotation(float rotation)
    18. {
    19.   elapsedTime = 0f;
    20.   goalVal = rotation;
    21.   startVal = myCachedTransform.localEulerAngles.y
    22. }
    23.  
    What's happening here: We keep track of the elapsed Time and use that value (kept between 0 and 1) to Linear interpolate between a start and a goal value.
     
    Vaupell likes this.
  3. Vaupell

    Vaupell

    Joined:
    Dec 2, 2013
    Posts:
    301
    Thanks for the tip..
    Then i can reply, first of all i am not using Getcomponent in update.. But i call the function when a bool is true.
    In this case,

    If pitch is true, then run the function once.
    in the function the pitch is set back to false, So it's only called once ;)
     
  4. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Perfect. Just wanted to make sure. Let me know how the rest goes.
     
    Vaupell likes this.