Search Unity

How Can I make A Platform Rotate 180degrees then Stop and Reset back to 0

Discussion in '2D' started by ryannicholasbecker, Nov 18, 2020.

  1. ryannicholasbecker

    ryannicholasbecker

    Joined:
    Oct 10, 2020
    Posts:
    5
    I want to make a platform that when a timer hits 0 it rotates 180 degrees and then stops, and then when the timer resets it starts all over again. I've tried using Quaternions but I don't think I know how to use them well enough and or how to use use euler angles with them, help please
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Just an euler angles rotation on z axis. To 180 and back to 0.

    Code (CSharp):
    1. public class NPCKilledCounter : MonoBehaviour
    2. {
    3.     [SerializeField] float _speed = 1;
    4.     bool _growingAngle = true;
    5.  
    6.     private void Update()
    7.     {
    8.         transform.Rotate((_growingAngle ? Vector3.forward : Vector3.back) * Time.deltaTime * _speed);
    9.  
    10.         if(_growingAngle && transform.rotation.eulerAngles.x >= 180)
    11.         {
    12.             _growingAngle = false;
    13.             return;
    14.         }
    15.         if (transform.rotation.eulerAngles.x <= 0) _growingAngle = true;
    16.     }
    17. }