Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class Player : MonoBehaviour
- {
- public LayerMask rayMask;
- public float Speed;
- public enum Direction {Left, Right, Forward, Back, Stay}
- public Direction MyDirection;
- public Vector3 nextDest;
- public Vector3 currentPos;
- public Vector3 lastPos;
- public float RotationTimer;
- public Animator anim;
- public bool disableRunAnimState;
- public bool StopRotation;
- public float rotationDelay;
- public Transform visibleTrEnemy;
- //
- public static float rotatePlayerSpeed = 5; //только визуально
- public Text rotSpeedText;
- //
- float steptime;
- //inactive
- public bool freeze;
- //[System.NonSerialized]
- public float Teleport;
- public Transform Cam;
- float movementtimer;
- bool changedir;
- //Для квестов
- [System.NonSerialized]
- public float ForwardSteps;
- void Start ()
- {
- nextDest = transform.position;
- currentPos = transform.position;
- }
- public void MoveForward()
- {
- if (CheckNextMove (transform.forward) == true) {
- if (MyDirection == Direction.Back) {
- rotationDelay = 0.3f;
- changedir = true;
- }
- MyDirection = Direction.Forward;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- } else
- {
- if (RotationTimer < 0.1f) {
- if (MyDirection == Direction.Right || MyDirection == Direction.Left) {
- if (CheckCurrentMove (transform.forward) == true) {
- MyDirection = Direction.Forward;
- nextDest = currentPos + new Vector3 (0, 0, 1);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
- }
- RotationTimer = 0;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- }
- }
- }
- }
- }
- public void MoveBack()
- {
- if (CheckNextMove (-transform.forward) == true)
- {
- if (MyDirection == Direction.Forward)
- {
- rotationDelay = 0.3f;
- changedir = true;
- }
- MyDirection = Direction.Back;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- }
- else
- {
- if (RotationTimer < 0.1f) {
- if (MyDirection == Direction.Right || MyDirection == Direction.Left) {
- if (CheckCurrentMove (-transform.forward) == true) {
- MyDirection = Direction.Back;
- nextDest = currentPos + new Vector3 (0, 0, -1);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
- }
- RotationTimer = 0;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- }
- }
- }
- }
- }
- public void MoveRight()
- {
- if (CheckNextMove (transform.right) == true) {
- if (MyDirection == Direction.Left) {
- rotationDelay = 0.3f;
- changedir = true;
- }
- MyDirection = Direction.Right;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- } else
- {
- if (RotationTimer < 0.1f)
- {
- if (MyDirection == Direction.Forward || MyDirection == Direction.Back) {
- if (CheckCurrentMove (transform.right) == true) {
- MyDirection = Direction.Right;
- nextDest = currentPos + new Vector3 (1, 0, 0);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
- }
- RotationTimer = 0;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- }
- }
- }
- }
- }
- public void MoveLeft()
- {
- if (CheckNextMove (-transform.right) == true)
- {
- if (MyDirection == Direction.Right)
- {
- rotationDelay = 0.3f;
- changedir = true;
- }
- MyDirection = Direction.Left;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- }else
- {
- if (RotationTimer < 0.1f)
- {
- if (MyDirection == Direction.Forward || MyDirection == Direction.Back) {
- if (CheckCurrentMove (-transform.right) == true) {
- MyDirection = Direction.Left;
- nextDest = currentPos + new Vector3 (-1, 0, 0);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
- }
- RotationTimer = 0;
- disableRunAnimState = false;
- anim.SetBool ("Run", true);
- }
- }
- }
- }
- }
- public void SwipeDirection(int dirNumber) //1-forward, 2-back, 3-right, 4-left
- {
- if (RotationTimer < 0.2f)
- {
- if (MyDirection == Direction.Forward || MyDirection == Direction.Back)
- {
- if (dirNumber==3&&CheckCurrentMove (transform.right) == true)
- {
- MyDirection = Direction.Right;
- nextDest = currentPos + new Vector3 (1, 0, 0);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
- }
- RotationTimer = 0;
- // anim.SetBool ("Run", true);
- }
- if (dirNumber==4&&CheckCurrentMove (-transform.right) == true)
- {
- MyDirection = Direction.Left;
- nextDest = currentPos + new Vector3 (-1, 0, 0);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
- }
- RotationTimer = 0;
- // anim.SetBool ("Run", true);
- }
- }
- if (MyDirection == Direction.Right || MyDirection == Direction.Left)
- {
- if (dirNumber==1&&CheckCurrentMove (transform.forward) == true)
- {
- MyDirection = Direction.Forward;
- nextDest = currentPos + new Vector3 (0, 0, 1);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
- }
- RotationTimer = 0;
- // anim.SetBool ("Run", true);
- }
- if (dirNumber==2&&CheckCurrentMove (-transform.forward) == true)
- {
- MyDirection = Direction.Back;
- nextDest = currentPos + new Vector3 (0, 0, -1);
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
- }
- RotationTimer = 0;
- // anim.SetBool ("Run", true);
- }
- }
- }
- }
- void Update ()
- {
- if (rotationDelay > 0)
- {
- rotationDelay -= Time.deltaTime;
- }
- RotationTimer += Time.deltaTime;
- //Forward
- if (Input.GetKeyDown (KeyCode.W)||Input.GetKeyDown(KeyCode.UpArrow))
- {
- MoveForward ();
- }
- if (MyDirection==Direction.Forward)
- {
- if (CheckNextMove (transform.forward) == true)
- {
- if (currentPos == nextDest)
- {
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
- }
- nextDest = currentPos + new Vector3 (0, 0, 1);
- RotationTimer = 0;
- }
- }
- else
- {
- disableRunAnimState = true;
- // anim.SetBool ("Run", false);
- }
- }
- //Back
- if (Input.GetKeyDown (KeyCode.S)||Input.GetKeyDown(KeyCode.DownArrow))
- {
- MoveBack ();
- }
- if (MyDirection==Direction.Back)
- {
- if (CheckNextMove (-transform.forward) == true)
- {
- if (currentPos == nextDest)
- {
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
- }
- nextDest = currentPos + new Vector3 (0, 0, -1);
- RotationTimer = 0;
- }
- }
- else
- {
- disableRunAnimState = true;
- //anim.SetBool ("Run", false);
- }
- }
- //Right
- if (Input.GetKeyDown (KeyCode.D)||Input.GetKeyDown(KeyCode.RightArrow))
- {
- MoveRight ();
- }
- if (MyDirection==Direction.Right)
- {
- if (CheckNextMove (transform.right) == true)
- {
- if (currentPos == nextDest)
- {
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
- }
- nextDest = currentPos + new Vector3 (1, 0, 0);
- RotationTimer = 0;
- }
- }
- else
- {
- disableRunAnimState = true;
- // anim.SetBool ("Run", false);
- }
- }
- //Left
- if (Input.GetKeyDown (KeyCode.A)||Input.GetKeyDown(KeyCode.LeftArrow))
- {
- MoveLeft ();
- }
- if (MyDirection==Direction.Left)
- {
- if (CheckNextMove (-transform.right) == true)
- {
- if (currentPos == nextDest)
- {
- if (Settings.RotatePlayer==false) {
- visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
- }
- nextDest = currentPos + new Vector3 (-1, 0, 0);
- RotationTimer = 0;
- }
- }
- else
- {
- disableRunAnimState = true;
- // anim.SetBool ("Run", false);
- }
- }
- //Поворот даже если игрок опоздал немного со свайпом (только для пк)
- /*
- if (RotationTimer < 1f)
- {
- if (MyDirection == Direction.Forward || MyDirection == Direction.Back)
- {
- if (Input.GetKeyDown (KeyCode.D)&&CheckCurrentMove (transform.right) == true)
- {
- Debug.Log ("rotate");
- MyDirection = Direction.Right;
- nextDest = currentPos + new Vector3 (1, 0, 0);
- RotationTimer = 0;
- }
- if (Input.GetKeyDown (KeyCode.A)&&CheckCurrentMove (-transform.right) == true)
- {
- Debug.Log ("rotate");
- MyDirection = Direction.Left;
- nextDest = currentPos + new Vector3 (-1, 0, 0);
- RotationTimer = 0;
- }
- }
- if (MyDirection == Direction.Right || MyDirection == Direction.Left)
- {
- if (Input.GetKeyDown (KeyCode.W)&&CheckCurrentMove (transform.forward) == true)
- {
- Debug.Log ("rotate");
- MyDirection = Direction.Forward;
- nextDest = currentPos + new Vector3 (0, 0, 1);
- RotationTimer = 0;
- }
- if (Input.GetKeyDown (KeyCode.S)&&CheckCurrentMove (-transform.forward) == true)
- {
- Debug.Log ("rotate");
- MyDirection = Direction.Back;
- nextDest = currentPos + new Vector3 (0, 0, -1);
- RotationTimer = 0;
- }
- }
- }*/
- transform.position = Vector3.MoveTowards (transform.position, nextDest, Speed * Time.deltaTime);
- /*
- movementtimer+=Time.deltaTime;
- if (movementtimer > 1 / Speed || changedir) { //0.5f
- changedir = false;
- transform.position = nextDest;
- movementtimer = 0;
- }
- */
- if(transform.position==nextDest)
- {
- if (Teleport != 0)
- {
- Vector3 pos = transform.position;
- pos.x += Teleport;
- transform.position = pos;
- Vector3 pos2 = anim.transform.parent.position;
- pos2.x += Teleport;
- anim.transform.parent.position = pos2;
- Vector3 pos3 = Cam.position;
- pos3.x += Teleport;
- Cam.GetComponent<CameraFollow> ().curpos.x += Teleport;
- Cam.position = pos3;
- Teleport = 0;
- nextDest = transform.position;
- }
- steptime = 0;
- lastPos = currentPos;
- currentPos = nextDest;
- //Для квеста
- ForwardSteps = transform.position.z + 2.5f;
- if (disableRunAnimState) {
- anim.SetBool ("Run", false);
- }
- }
- visibleTrEnemy.position = Vector3.MoveTowards (visibleTrEnemy.position, transform.position, Time.deltaTime * Speed);
- if (Settings.RotatePlayer) {
- {
- Vector3 vector = visibleTrEnemy.position - currentPos;
- if (vector != Vector3.zero) {
- var targetRotation = Quaternion.LookRotation (vector, Vector3.up);
- if (rotationDelay > 0)
- {
- visibleTrEnemy.rotation = Quaternion.Lerp (visibleTrEnemy.rotation, Quaternion.LookRotation (vector), 25 * Time.deltaTime);
- }
- else
- {
- visibleTrEnemy.rotation = Quaternion.Lerp (visibleTrEnemy.rotation, Quaternion.LookRotation (vector), Time.deltaTime * rotatePlayerSpeed);
- }
- }
- }
- }
- }
- public bool CheckNextMove(Vector3 destination)
- {
- RaycastHit hit;
- //if (Physics.Raycast (transform.position, destination, out hit, 1))
- Vector3 RayStartPos = nextDest + destination + new Vector3 (0,1,0);
- Debug.DrawRay(RayStartPos, -Vector3.up*3);
- if (Physics.Raycast (RayStartPos, -Vector3.up, out hit, 3,rayMask))
- {
- if (hit.transform.CompareTag ("Obstacle"))
- {
- return false;
- } else
- {
- return true;
- }
- } else
- {
- return false;
- }
- return true;
- }
- public bool CheckCurrentMove(Vector3 destination)
- {
- RaycastHit hit;
- //if (Physics.Raycast (transform.position, destination, out hit, 1))
- Vector3 RayStartPos = currentPos + destination+ new Vector3 (0,1,0);
- if (Physics.Raycast (RayStartPos, -Vector3.up, out hit, 3, rayMask)) {
- if (hit.transform.CompareTag ("Obstacle")) {
- return false;
- } else {
- return true;
- }
- } else
- {
- return false;
- }
- return true;
- }
- public void RotateSpeedControlViaSettings(float rotSpeed)
- {
- rotatePlayerSpeed = rotSpeed;
- rotSpeedText.text = rotatePlayerSpeed.ToString ("F0");
- }
- public void OnDrawGizmos()
- {
- // Gizmos.DrawSphere (nextDest + transform.forward + new Vector3 (0,1,0),0.5f);
- }
- }
Add Comment
Please, Sign In to add comment