Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.07 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Flipper : MonoBehaviour
  6. {
  7.     // Start is called before the first frame update
  8.     [SerializeField]
  9.     private float swingSpeed = 0.35f;
  10.     [SerializeField]
  11.     private float swingPower = 30f;
  12.     [SerializeField]
  13.     private float swingDistance = 45f;
  14.     [SerializeField]
  15.     private bool leftSide = false;
  16.     [SerializeField]
  17.     private KeyCode keyPress = KeyCode.Space;
  18.  
  19.     private Quaternion startRotation;
  20.     private Quaternion endRotation;
  21.     private float currentProgress;
  22.  
  23.     enum FlipperState
  24.     {
  25.         Idle,
  26.         Swinging,
  27.         Returning,
  28.     }
  29.     private FlipperState currentState = FlipperState.Idle;
  30.  
  31.     void Start()
  32.     {
  33.         startRotation = transform.rotation;
  34.  
  35.         if( leftSide )
  36.         {
  37.             swingDistance = -swingDistance;
  38.  
  39.         }
  40.         endRotation = Quaternion.Euler( new Vector3( 0, startRotation.eulerAngles.y + swingDistance, 0 ) );
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         if( Input.GetKeyDown( keyPress ) )
  47.         {
  48.             if( currentState != FlipperState.Idle )
  49.             {
  50.                 Debug.Log( "Cannot Swing Flipper, already in Swinging state" );
  51.  
  52.             }
  53.             else
  54.             {
  55.                 StartSwing();
  56.             }
  57.         }
  58.         if( currentState == FlipperState.Swinging )
  59.         {
  60.            
  61.             currentProgress += Time.deltaTime;
  62.             //Debug.Log( "Swinging " + currentProgress );
  63.             //transform.rotation = Quaternion.Lerp( startRotation, endRotation, currentProgress/ swingSpeed );
  64.             if( currentProgress >= swingSpeed )
  65.             {
  66.                 currentState = FlipperState.Returning;
  67.                 currentProgress = 0;
  68.             }
  69.         }
  70.         else if( currentState == FlipperState.Returning )
  71.         {
  72.             currentProgress += Time.deltaTime;
  73.             //Debug.Log( "Returning " + currentProgress );
  74.             //transform.rotation = Quaternion.Lerp( endRotation, startRotation, currentProgress / swingSpeed );
  75.             if( currentProgress >= swingSpeed )
  76.             {
  77.                 currentState = FlipperState.Idle;
  78.                 currentProgress = 0;
  79.             }
  80.         }
  81.     }
  82.  
  83.     private void StartSwing()
  84.     {
  85.         currentState = FlipperState.Swinging;
  86.         currentProgress = 0;
  87.         GetComponent<Rigidbody>().AddTorque( Vector3.up * swingPower );
  88.  
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement