Advertisement
leomovskii

CatController

Dec 9th, 2023 (edited)
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(Rigidbody2D))]
  4. public class CatController : MonoBehaviour {
  5.  
  6.     public float moveSpeed;
  7.     public float runSpeed;
  8.    
  9.     Rigidbody2D rb;
  10.     Vector2 moveVector;
  11.     bool isRunning;
  12.  
  13.     private void Start() {
  14.         rb = GetComponent<Rigidbody2D>();
  15.     }
  16.  
  17.     private void Update() {
  18.         float x = Input.GetAxisRaw("Horizontal"); // -1, 0, 1
  19.         float y = Input.GetAxisRaw("Vertical"); // -1, 0, 1
  20.  
  21.         moveVector = new Vector2(x, y).normalized;
  22.         isRunning = Input.GetKey(KeyCode.LeftShift);
  23.  
  24.         if (x > 0) {
  25.             transform.localEulerAngles = new Vector3(0, 0, 0);
  26.         } else if (x < 0) {
  27.             transform.localEulerAngles = new Vector3(0, 180, 0);
  28.         }
  29.     }
  30.  
  31.     private void FixedUpdate() { // 0.02s
  32.         if (isRunning) {
  33.             rb.velocity = moveVector * runSpeed;
  34.         } else {
  35.             rb.velocity = moveVector * moveSpeed;
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement