Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CubeMover : MonoBehaviour {
  6.  
  7.     public float moveTime = 0.5f;
  8.  
  9.     Vector3 startPosition;
  10.     Vector3 targetPosition;
  11.     float targetTime;
  12.  
  13.     void Move(Vector3 move) {
  14.  
  15.         targetTime = Time.time + moveTime;
  16.         startPosition = transform.position;
  17.         targetPosition = transform.position + move;
  18.  
  19.     }
  20.  
  21.     void Update() {
  22.  
  23.         if(targetTime > Time.time) {
  24.  
  25.             transform.position = Vector3.Lerp(startPosition, targetPosition, 1 - (targetTime - Time.time) / moveTime);
  26.  
  27.         } else {
  28.  
  29.             if(Input.GetKey(KeyCode.UpArrow)) {
  30.                 Move(Vector3.forward);
  31.             } else if(Input.GetKey(KeyCode.DownArrow)) {
  32.                 Move(Vector3.back);
  33.             } else if(Input.GetKey(KeyCode.LeftArrow)) {
  34.                 Move(Vector3.left);
  35.             } else if(Input.GetKey(KeyCode.RightArrow)) {
  36.                 Move(Vector3.right);
  37.             }
  38.  
  39.         }
  40.  
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement