Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Flipper : MonoBehaviour
- {
- // Start is called before the first frame update
- [SerializeField]
- private float swingSpeed = 0.35f;
- [SerializeField]
- private float swingPower = 30f;
- [SerializeField]
- private float swingDistance = 45f;
- [SerializeField]
- private bool leftSide = false;
- [SerializeField]
- private KeyCode keyPress = KeyCode.Space;
- private Quaternion startRotation;
- private Quaternion endRotation;
- private float currentProgress;
- enum FlipperState
- {
- Idle,
- Swinging,
- Returning,
- }
- private FlipperState currentState = FlipperState.Idle;
- void Start()
- {
- startRotation = transform.rotation;
- if( leftSide )
- {
- swingDistance = -swingDistance;
- }
- endRotation = Quaternion.Euler( new Vector3( 0, startRotation.eulerAngles.y + swingDistance, 0 ) );
- }
- // Update is called once per frame
- void Update()
- {
- if( Input.GetKeyDown( keyPress ) )
- {
- if( currentState != FlipperState.Idle )
- {
- Debug.Log( "Cannot Swing Flipper, already in Swinging state" );
- }
- else
- {
- StartSwing();
- }
- }
- if( currentState == FlipperState.Swinging )
- {
- currentProgress += Time.deltaTime;
- //Debug.Log( "Swinging " + currentProgress );
- //transform.rotation = Quaternion.Lerp( startRotation, endRotation, currentProgress/ swingSpeed );
- if( currentProgress >= swingSpeed )
- {
- currentState = FlipperState.Returning;
- currentProgress = 0;
- }
- }
- else if( currentState == FlipperState.Returning )
- {
- currentProgress += Time.deltaTime;
- //Debug.Log( "Returning " + currentProgress );
- //transform.rotation = Quaternion.Lerp( endRotation, startRotation, currentProgress / swingSpeed );
- if( currentProgress >= swingSpeed )
- {
- currentState = FlipperState.Idle;
- currentProgress = 0;
- }
- }
- }
- private void StartSwing()
- {
- currentState = FlipperState.Swinging;
- currentProgress = 0;
- GetComponent<Rigidbody>().AddTorque( Vector3.up * swingPower );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement