Advertisement
Guest User

Untitled

a guest
Apr 28th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [RequireComponent(typeof(PlayerController))]
  6. public class PlayerInput : MonoBehaviour
  7. {
  8.     public KeyCode forward = KeyCode.W;
  9.     public KeyCode back = KeyCode.S;
  10.     public KeyCode left = KeyCode.A;
  11.     public KeyCode right = KeyCode.D;
  12.     public KeyCode turnLeft = KeyCode.Q;
  13.     public KeyCode turnRight = KeyCode.E;
  14.  
  15.     PlayerController controller;
  16.  
  17.     private void Awake()
  18.     {
  19.         controller = GetComponent<PlayerController>();
  20.     }
  21.  
  22.     private void Update()
  23.     {
  24.         if (Input.GetKeyUp(forward)) controller.MoveForward();
  25.         if (Input.GetKeyUp(back)) controller.MoveBackward();
  26.         if (Input.GetKeyUp(left)) controller.MoveLeft();
  27.         if (Input.GetKeyUp(right)) controller.MoveRight();
  28.         if (Input.GetKeyUp(turnLeft)) controller.RotateLeft();
  29.         if (Input.GetKeyUp(turnRight)) controller.RotateRight();
  30.     }
  31. }
  32.  
  33.  
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using UnityEngine;
  37.  
  38. public class PlayerController : MonoBehaviour
  39. {
  40.     public bool smoothTransition = true;
  41.     public float transitionSpeed = 10f;
  42.     public float transitionRotarionSpeed = 500f;
  43.  
  44.     Vector3 targetGridPos;
  45.     Vector3 prevTargetGridPos;
  46.     Vector3 targetRotation;
  47.  
  48.     bool AtRest
  49.     {
  50.         get
  51.         {
  52.             if ((Vector3.Distance(transform.position, targetGridPos) < 0.05f) &&
  53.                 (Vector3.Distance(transform.eulerAngles, targetRotation) < 0.05f))
  54.                 return true;
  55.             else
  56.                 return false;
  57.         }
  58.     }
  59.  
  60.     public void RotateLeft() { if (AtRest) targetRotation -= Vector3.up * 90f; }
  61.     public void RotateRight() { if (AtRest) targetRotation += Vector3.up * 90f; }
  62.     public void MoveForward() { if (AtRest) targetGridPos += transform.forward; }
  63.     public void MoveBackward() { if (AtRest) targetGridPos -= transform.forward; }
  64.     public void MoveLeft() { if (AtRest) targetGridPos -= transform.right; }
  65.     public void MoveRight() { if (AtRest) targetGridPos += transform.right; }
  66.  
  67.     private void Start()
  68.     {
  69.         targetGridPos = Vector3Int.RoundToInt(transform.position);
  70.     }
  71.  
  72.     private void FixedUpdate()
  73.     {
  74.         MovePlayer();
  75.     }
  76.  
  77.     void MovePlayer()
  78.     {
  79.         if (Mathf.RoundToInt(targetGridPos.x)%2 ==0 || Mathf.RoundToInt(targetGridPos.z) % 2 == 0)
  80.         {
  81.             prevTargetGridPos = targetGridPos;
  82.  
  83.             Vector3 targetPosition = targetGridPos;
  84.  
  85.             if (targetRotation.y > 270f && targetRotation.y < 361f) targetRotation.y = 0f;
  86.             if (targetRotation.y < 0f) targetRotation.y = 270f;
  87.  
  88.             if (!smoothTransition)
  89.             {
  90.                 transform.position = targetPosition;
  91.                 transform.rotation = Quaternion.Euler(targetRotation);
  92.             }
  93.             else
  94.             {
  95.                 transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * transitionSpeed);
  96.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), Time.deltaTime * transitionRotarionSpeed);
  97.             }
  98.         }
  99.         else
  100.         {
  101.             targetGridPos = prevTargetGridPos;
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement