Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- // @kurtdekker
- public class SmoothMovement : MonoBehaviour
- {
- float currentQuantity;
- float desiredQuantity;
- const float MovementPerSecond = 2.0f;
- void Start ()
- {
- // TODO: set up your initial values, if any
- currentQuantity = 1;
- // match desired
- desiredQuantity = currentQuantity;
- }
- void AcceptUserInput()
- {
- if (Input.GetKeyDown( KeyCode.Alpha1))
- {
- desiredQuantity = 1;
- }
- if (Input.GetKeyDown( KeyCode.Alpha2))
- {
- desiredQuantity = 2;
- }
- if (Input.GetKeyDown( KeyCode.Alpha3))
- {
- desiredQuantity = 3;
- }
- }
- void ProcessMovement()
- {
- // Every frame without exception move the currentQuantity
- // towards the desiredQuantity, by the movement rate:
- currentQuantity = Mathf.MoveTowards(
- currentQuantity,
- desiredQuantity,
- MovementPerSecond * Time.deltaTime);
- }
- public UnityEngine.UI.Text TextOutput;
- void DisplayResults()
- {
- TextOutput.text = "Press 1, 2 or 3 to select desiredQuantity.\n\n" +
- "desiredQuantity = " + desiredQuantity + "\n" +
- "currentQuantity = " + currentQuantity + "\n";
- }
- void Update ()
- {
- AcceptUserInput();
- ProcessMovement();
- DisplayResults();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement