Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour {
- // Start is called before the first frame update
- public int speed = 1;
- private Animator anim;
- private float timeSinceMovedLeft;
- private bool movingLeft = false;
- private bool movingRight = false;
- private bool rotatedLeftEnough = false;
- private bool rotatedRightEnough = false;
- void Start() {
- anim = GetComponent<Animator>();
- }
- void clearAnimations() {
- anim.SetBool("isWalking", false);
- }
- void clearMovements() {
- transform.rotation = Quaternion.Euler(0, 0, 0);
- rotatedLeftEnough = false;
- rotatedRightEnough = false;
- }
- void MoveFoward() {
- anim.SetBool("isWalking", true);
- transform.Translate(Vector3.forward * speed * Time.deltaTime);
- }
- void MoveLeft() {
- if (movingLeft) {
- timeSinceMovedLeft += Time.deltaTime;
- if (timeSinceMovedLeft > 0.2) {
- Quaternion rotateAmount = Quaternion.Euler(0, -90f, 0f);
- float angle = Quaternion.Angle(transform.rotation, rotateAmount);
- if (angle >= 0 && angle <= 10) {
- rotatedLeftEnough = true;
- }
- if (rotatedLeftEnough) {
- MoveFoward();
- } else {
- transform.Rotate(0f, -5f, 0f);
- }
- }
- }
- movingLeft = true;
- return;
- }
- void MoveRight() {
- if (movingRight) {
- timeSinceMovedLeft += Time.deltaTime;
- if (timeSinceMovedLeft > 0.2) {
- Quaternion rotateAmount = Quaternion.Euler(0, 90f, 0f);
- float angle = Quaternion.Angle(transform.rotation, rotateAmount);
- if (angle >= 0 && angle <= 10) {
- rotatedRightEnough = true;
- }
- if (rotatedRightEnough) {
- MoveFoward();
- } else {
- transform.Rotate(0f, 5f, 0f);
- }
- }
- }
- movingRight = true;
- return;
- }
- void SetAnimations() {
- if (Input.GetKey("w")) {
- MoveFoward();
- return;
- }
- if (Input.GetKey("d")) {
- MoveRight();
- return;
- }
- if (Input.GetKey("a")) {
- MoveLeft();
- return;
- }
- clearAnimations();
- clearMovements();
- return;
- }
- void Update() {
- SetAnimations();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement