Advertisement
halleman19

smooth movement and rotate | unity3d

Oct 8th, 2023
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | Gaming | 0 0
  1. using UnityEngine;
  2.  
  3. public class smoothMovementAndRotate : MonoBehaviour
  4. {
  5.     // 1с == 1 квадрат при швидкості 1.
  6.     // 0.5 швидкість, 2с часу == 1 квадрат пропливає
  7.  
  8.     public float defSpeed = 1;
  9.     public float speedRotate = 1;
  10.     public float speedMove = 1;
  11.  
  12.     public float timeTurn = 2;
  13.  
  14.     private void Start()
  15.     {
  16.         Invoke("resetTurn", timeTurn);
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         if (Input.GetKeyDown(KeyCode.Q))
  22.             setSpeedRotate(-defSpeed);
  23.  
  24.         if (Input.GetKeyDown(KeyCode.E))
  25.             setSpeedRotate(defSpeed);
  26.  
  27.         transform.Translate(new Vector3(speedRotate, 0, -speedMove) * Time.deltaTime);
  28.     }
  29.  
  30.     private void resetTurn()
  31.     {
  32.         speedRotate = 0;
  33.     }
  34.  
  35.     private void setSpeedRotate(float speed)
  36.     {
  37.         speedRotate = speed > 0 ? Mathf.Abs(speed) : -Mathf.Abs(speed);
  38.         Invoke("resetTurn", timeTurn);
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement