Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent(typeof(PlayerController))]
- public class PlayerInput : MonoBehaviour
- {
- public KeyCode forward = KeyCode.W;
- public KeyCode back = KeyCode.S;
- public KeyCode left = KeyCode.A;
- public KeyCode right = KeyCode.D;
- public KeyCode turnLeft = KeyCode.Q;
- public KeyCode turnRight = KeyCode.E;
- PlayerController controller;
- private void Awake()
- {
- controller = GetComponent<PlayerController>();
- }
- private void Update()
- {
- if (Input.GetKeyUp(forward)) controller.MoveForward();
- if (Input.GetKeyUp(back)) controller.MoveBackward();
- if (Input.GetKeyUp(left)) controller.MoveLeft();
- if (Input.GetKeyUp(right)) controller.MoveRight();
- if (Input.GetKeyUp(turnLeft)) controller.RotateLeft();
- if (Input.GetKeyUp(turnRight)) controller.RotateRight();
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public bool smoothTransition = true;
- public float transitionSpeed = 10f;
- public float transitionRotarionSpeed = 500f;
- Vector3 targetGridPos;
- Vector3 prevTargetGridPos;
- Vector3 targetRotation;
- bool AtRest
- {
- get
- {
- if ((Vector3.Distance(transform.position, targetGridPos) < 0.05f) &&
- (Vector3.Distance(transform.eulerAngles, targetRotation) < 0.05f))
- return true;
- else
- return false;
- }
- }
- public void RotateLeft() { if (AtRest) targetRotation -= Vector3.up * 90f; }
- public void RotateRight() { if (AtRest) targetRotation += Vector3.up * 90f; }
- public void MoveForward() { if (AtRest) targetGridPos += transform.forward; }
- public void MoveBackward() { if (AtRest) targetGridPos -= transform.forward; }
- public void MoveLeft() { if (AtRest) targetGridPos -= transform.right; }
- public void MoveRight() { if (AtRest) targetGridPos += transform.right; }
- private void Start()
- {
- targetGridPos = Vector3Int.RoundToInt(transform.position);
- }
- private void FixedUpdate()
- {
- MovePlayer();
- }
- void MovePlayer()
- {
- if (Mathf.RoundToInt(targetGridPos.x)%2 ==0 || Mathf.RoundToInt(targetGridPos.z) % 2 == 0)
- {
- prevTargetGridPos = targetGridPos;
- Vector3 targetPosition = targetGridPos;
- if (targetRotation.y > 270f && targetRotation.y < 361f) targetRotation.y = 0f;
- if (targetRotation.y < 0f) targetRotation.y = 270f;
- if (!smoothTransition)
- {
- transform.position = targetPosition;
- transform.rotation = Quaternion.Euler(targetRotation);
- }
- else
- {
- transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * transitionSpeed);
- transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(targetRotation), Time.deltaTime * transitionRotarionSpeed);
- }
- }
- else
- {
- targetGridPos = prevTargetGridPos;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement