Advertisement
Guest User

Untitled

a guest
May 9th, 2021
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class MoveBall : MonoBehaviour
  5. {
  6.     public Vector3 MoveOffset;
  7.  
  8.     private float easeInOutQuad(float value)
  9.     {
  10.         value /= 0.5f;
  11.         if (value < 1) return 0.5f * value * value;
  12.         value--;
  13.         return -0.5f * (value * (value - 2.0f) - 1.0f);
  14.     }
  15.  
  16.     private Vector3 vStartPosition;
  17.     private float fAnimationPhase;
  18.     private float fAnimationSpeed;
  19.  
  20.     private bool bAnimating;
  21.     public IEnumerator Animate(Vector3 vTargetPosition)
  22.     {
  23.         bAnimating = true;
  24.         while (fAnimationPhase < 1.0f)
  25.         {
  26.             fAnimationPhase = Mathf.Clamp01(fAnimationPhase + Time.deltaTime * fAnimationSpeed);
  27.             float fEasedPhase = easeInOutQuad(fAnimationPhase);
  28.  
  29.             // apply animation
  30.             transform.position = Vector3.Lerp(vStartPosition, vTargetPosition, fEasedPhase);
  31.  
  32.             yield return null; // progress one frame
  33.         }
  34.         bAnimating = false;
  35.     }
  36.  
  37.     private void ExecuteAnimation(float animationSpeed)
  38.     {
  39.         if (!bAnimating)
  40.         {
  41.             Debug.Log("Animating");
  42.             fAnimationPhase = 0.0f;
  43.             fAnimationSpeed = animationSpeed;
  44.             vStartPosition = transform.position;
  45.  
  46.             StartCoroutine(Animate(vStartPosition + MoveOffset));
  47.         }
  48.     }
  49.  
  50.     // Use this for initialization
  51.     void Start()
  52.     {
  53.     }
  54.  
  55.     // Update is called once per frame
  56.     void Update()
  57.     {
  58.         // Left click (Mouse0) adds MoveOffset to the current position
  59.         if (Input.GetKeyDown(KeyCode.Mouse0))
  60.         {
  61.             ExecuteAnimation(1.0f);
  62.         }
  63.  
  64.         // Modify this class so that right click (Mouse1) causes the ball to
  65.         // move in the opposite direction while also moving half as fast.
  66.         // NOTE: You are not allowed to assign to MoveOffset, only use its value.
  67.         if (Input.GetKeyDown(KeyCode.Mouse1))
  68.         {
  69.  
  70.         }
  71.  
  72.         // Modify this class so that pressing the space bar doubles the speed of
  73.         // both movement animations. Pressing it again restores the speeds back
  74.         // to normal.
  75.         if (Input.GetKeyDown(KeyCode.Space))
  76.         {
  77.  
  78.         }
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement