Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class MoveBall : MonoBehaviour
- {
- public Vector3 MoveOffset;
- private float easeInOutQuad(float value)
- {
- value /= 0.5f;
- if (value < 1) return 0.5f * value * value;
- value--;
- return -0.5f * (value * (value - 2.0f) - 1.0f);
- }
- private Vector3 vStartPosition;
- private float fAnimationPhase;
- private float fAnimationSpeed;
- private bool bAnimating;
- public IEnumerator Animate(Vector3 vTargetPosition)
- {
- bAnimating = true;
- while (fAnimationPhase < 1.0f)
- {
- fAnimationPhase = Mathf.Clamp01(fAnimationPhase + Time.deltaTime * fAnimationSpeed);
- float fEasedPhase = easeInOutQuad(fAnimationPhase);
- // apply animation
- transform.position = Vector3.Lerp(vStartPosition, vTargetPosition, fEasedPhase);
- yield return null; // progress one frame
- }
- bAnimating = false;
- }
- private void ExecuteAnimation(float animationSpeed)
- {
- if (!bAnimating)
- {
- Debug.Log("Animating");
- fAnimationPhase = 0.0f;
- fAnimationSpeed = animationSpeed;
- vStartPosition = transform.position;
- StartCoroutine(Animate(vStartPosition + MoveOffset));
- }
- }
- // Use this for initialization
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- // Left click (Mouse0) adds MoveOffset to the current position
- if (Input.GetKeyDown(KeyCode.Mouse0))
- {
- ExecuteAnimation(1.0f);
- }
- // Modify this class so that right click (Mouse1) causes the ball to
- // move in the opposite direction while also moving half as fast.
- // NOTE: You are not allowed to assign to MoveOffset, only use its value.
- if (Input.GetKeyDown(KeyCode.Mouse1))
- {
- }
- // Modify this class so that pressing the space bar doubles the speed of
- // both movement animations. Pressing it again restores the speeds back
- // to normal.
- if (Input.GetKeyDown(KeyCode.Space))
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement