SoFunction
Updated on 2025-03-07

Simple notes on Unity’s implementation of a large turntable

This article shares the specific code for Unity implementation of the large turntable for your reference. The specific content is as follows

1. To rotate a gameobject in unity, we need to change the corresponding Rotation under its transform. Since our large turntable is a 2D perspective, we first need to clarify that the direction of the rotation of the large turntable is the Z of the rotating Rotation.

2. How to achieve the large turntable from rotation to slow, and then stop when the rotation is specified as the position. After checking the script of unity, you can find the following method to rotate the large turntable as follows:

public void Rotate(Vector3 eulerAngles, Space relativeTo = );

Apply an Euler angle of rotation, with degrees around the z-axis, degrees around the x-axis, and degrees around the y-axis (in this order).

public void Rotate(Vector3 axis, float angle, Space relativeTo = );

Rotate and transform around the axis axis according to angle degree.

public void RotateAround(Vector3 point, Vector3 axis, float angle);

Simply put, rotate an object at a certain position at the world coordinates according to the degree of degree

3. For rotary viewing unity script, you know that the above methods need to be implemented first in void Update(); So we implement it in the void Update() method:

//Whether to click the Start button
if (m_isStart == true)

{

    m_cStartButton.m_CurState = ;//The current start button status is disabled
    m_fTime += ;//Time timer accumulation    m_fVelocity = k / m_fTime;//Playing speed (we use the inverse proportion function to achieve the effect of speed from fast to slow)    if (m_fVelocity<=1)
    {
    
     float value = (m_iID*36.0f);//Here is a place to stop for my project     float diff = m_gRatePin. - value;//The Euler angle of the currently rotated gameobject     if ((diff)<=50)// If the rotation gameobject and the specified stop position are less than 50 degrees, the slow deceleration begins     {

      //Use spherical interpolation to allow the rotated gameobject to slowly move to the specified position      Quaternion quaternion = m_gRatePin.;
      m_gRatePin. = (quaternion, (0, 0, value), );
     
      if (m_gRatePin. == (0, 0, value))
      {
      m_isStart = false;
      m_cStartButton.m_CurState = ;
      m_fTime = 0;
      m_fVelocity = 36.0f;
      }     
     }
     else
     { 

     //Rotation (-1 means the direction is right)
      m_gRatePin.(, (-1) * m_fVelocity);
     }
    }
    else
    { //Rotation (-1 means the direction is right)     m_gRatePin.(, (-1) * m_fVelocity);
    }

}

4. In addition to using the rotation gameobject to achieve large turntable rotation, we can also use animation to implement the following:

if (m_isStart == true)       //Whether to click on the Start Lottery button  {
   m_cStartButton.m_CurState = ;
   m_fTime += ;     //Timer   if (m_fTime > 1 / m_fVelocity)    //Judge whether the current time has reached the time of playing one frame   {
    m_fTime -= 1 / m_fVelocity;
    if (m_fVelocity<=8f)     //Judge whether the speed is less than a certain value (set according to your own needs)    {
     if (index == m_iID)    //Discern whether the currently played frame is background control data. If so, stop playing and clear data     {
      m_gRatePin.GetComponent<SpriteRenderer>().sprite = m_sSprites[m_iID];
      m_isStart = false;
      m_fVelocity = 60.0f;
      m_fTime = 0.0f;
      m_fTimeMeter = 0.0f;
      m_iCount = 0;
      m_cStartButton.m_CurState = ;
     }
     else
     {
      //1 / m_fVelocity indicates how long it takes to play a frame      if (index == m_sSprites.Length - 1)    //If the current frame index is the last frame, it means that the next frame played is the first frame      {
       m_gRatePin.GetComponent<SpriteRenderer>().sprite = m_sSprites[0];
       index = 0;         //Reset the index       m_iCount++;         //Statistics circles       m_fVelocity /= 2;       //Slow down the playback frame speed      }
      else
      {
       //If not, continue to play the next frame       m_gRatePin.GetComponent<SpriteRenderer>().sprite = m_sSprites[++index];
      }
     }
    }
    else//If the specified circle is not reached, the next frame will continue to play.    {


     if (index == m_sSprites.Length - 1)
     {
      m_gRatePin.GetComponent<SpriteRenderer>().sprite = m_sSprites[0];
      index = 0;
      m_iCount++;
      m_fVelocity /= 2;
     }
     else 
     {
      m_gRatePin.GetComponent<SpriteRenderer>().sprite = m_sSprites[++index];
     }


    }
   }
  }

5. In fact, there are quite a few methods, such as using the animation system brought by Unit, which can be achieved. The notes are summarized here.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.