AndrewRosyaev

Player 0.72

Jan 31st, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     public LayerMask rayMask;
  8.     public float Speed;
  9.     public enum Direction {Left, Right, Forward, Back, Stay}
  10.     public Direction MyDirection;
  11.     public Vector3 nextDest;
  12.     public Vector3 currentPos;
  13.     public Vector3 lastPos;
  14.     public float RotationTimer;
  15.     public Animator anim;
  16.     public bool disableRunAnimState;
  17.     public bool StopRotation;
  18.     public float rotationDelay;
  19.     public Transform visibleTrEnemy;
  20.     //
  21.     public static float rotatePlayerSpeed = 5; //только визуально
  22.     public Text rotSpeedText;
  23.     //
  24.  
  25.     float steptime;
  26.     //inactive
  27.     public bool freeze;
  28.     //[System.NonSerialized]
  29.     public float Teleport;
  30.     public Transform Cam;
  31.     float movementtimer;
  32.     bool changedir;
  33.  
  34.     //Для квестов
  35.     [System.NonSerialized]
  36.     public float ForwardSteps;
  37.  
  38.  
  39.     void Start ()
  40.     {
  41.         nextDest = transform.position;
  42.         currentPos = transform.position;
  43.     }
  44.  
  45.  
  46.     public void MoveForward()
  47.     {
  48.         if (CheckNextMove (transform.forward) == true) {
  49.             if (MyDirection == Direction.Back) {
  50.                 rotationDelay = 0.3f;
  51.                 changedir = true;
  52.             }
  53.  
  54.             MyDirection = Direction.Forward;
  55.             disableRunAnimState = false;   
  56.             anim.SetBool ("Run", true);
  57.         } else
  58.         {
  59.             if (RotationTimer < 0.1f)  {
  60.                 if (MyDirection == Direction.Right || MyDirection == Direction.Left) {
  61.                     if (CheckCurrentMove (transform.forward) == true) {
  62.                         MyDirection = Direction.Forward;   
  63.                         nextDest = currentPos + new Vector3 (0, 0, 1);
  64.                         if (Settings.RotatePlayer==false) {
  65.                             visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
  66.                         }
  67.                         RotationTimer = 0;
  68.                         disableRunAnimState = false;   
  69.                         anim.SetBool ("Run", true);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     public void MoveBack()
  76.     {
  77.         if (CheckNextMove (-transform.forward) == true)
  78.         {
  79.             if (MyDirection == Direction.Forward)
  80.             {
  81.                 rotationDelay = 0.3f;
  82.                 changedir = true;
  83.             }
  84.  
  85.  
  86.             MyDirection = Direction.Back;
  87.             disableRunAnimState = false;
  88.             anim.SetBool ("Run", true);
  89.         }
  90.         else
  91.         {
  92.             if (RotationTimer < 0.1f)  {
  93.                 if (MyDirection == Direction.Right || MyDirection == Direction.Left) {
  94.                     if (CheckCurrentMove (-transform.forward) == true) {
  95.                         MyDirection = Direction.Back;  
  96.                         nextDest = currentPos + new Vector3 (0, 0, -1);
  97.                         if (Settings.RotatePlayer==false) {
  98.                             visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
  99.                         }
  100.                         RotationTimer = 0;
  101.                         disableRunAnimState = false;   
  102.                         anim.SetBool ("Run", true);
  103.                     }
  104.                 }
  105.             }
  106.         }
  107.     }
  108.     public void MoveRight()
  109.     {
  110.         if (CheckNextMove (transform.right) == true) {
  111.             if (MyDirection == Direction.Left) {
  112.                 rotationDelay = 0.3f;
  113.                 changedir = true;
  114.             }
  115.             MyDirection = Direction.Right;
  116.             disableRunAnimState = false;
  117.             anim.SetBool ("Run", true);
  118.         } else
  119.         {
  120.             if (RotationTimer < 0.1f)
  121.             {
  122.                 if (MyDirection == Direction.Forward || MyDirection == Direction.Back) {
  123.                     if (CheckCurrentMove (transform.right) == true) {
  124.                         MyDirection = Direction.Right; 
  125.                         nextDest = currentPos + new Vector3 (1, 0, 0);
  126.                         if (Settings.RotatePlayer==false) {
  127.                             visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
  128.                         }
  129.                         RotationTimer = 0;
  130.                         disableRunAnimState = false;   
  131.                         anim.SetBool ("Run", true);
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137.     public void MoveLeft()
  138.     {
  139.         if (CheckNextMove (-transform.right) == true)
  140.         {
  141.             if (MyDirection == Direction.Right)
  142.             {
  143.                 rotationDelay = 0.3f;
  144.                 changedir = true;
  145.             }
  146.             MyDirection = Direction.Left;
  147.             disableRunAnimState = false;
  148.             anim.SetBool ("Run", true);
  149.         }else
  150.         {
  151.             if (RotationTimer < 0.1f)
  152.             {
  153.                 if (MyDirection == Direction.Forward || MyDirection == Direction.Back) {
  154.                     if (CheckCurrentMove (-transform.right) == true) {
  155.                         MyDirection = Direction.Left;  
  156.                         nextDest = currentPos + new Vector3 (-1, 0, 0);
  157.                         if (Settings.RotatePlayer==false) {
  158.                             visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
  159.                         }
  160.                         RotationTimer = 0;
  161.                         disableRunAnimState = false;   
  162.                         anim.SetBool ("Run", true);
  163.                     }
  164.                 }
  165.             }
  166.         }
  167.     }
  168.  
  169.     public void SwipeDirection(int dirNumber) //1-forward, 2-back, 3-right, 4-left
  170.     {
  171.         if (RotationTimer < 0.2f)
  172.         {
  173.             if (MyDirection == Direction.Forward || MyDirection == Direction.Back)
  174.             {
  175.                 if (dirNumber==3&&CheckCurrentMove (transform.right) == true)
  176.                 {
  177.                     MyDirection = Direction.Right; 
  178.                     nextDest = currentPos + new Vector3 (1, 0, 0);
  179.                     if (Settings.RotatePlayer==false) {
  180.                         visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
  181.                     }
  182.                     RotationTimer = 0;
  183.                 //  anim.SetBool ("Run", true);
  184.                 }
  185.                 if (dirNumber==4&&CheckCurrentMove (-transform.right) == true)
  186.                 {
  187.                     MyDirection = Direction.Left;  
  188.                     nextDest = currentPos + new Vector3 (-1, 0, 0);
  189.                     if (Settings.RotatePlayer==false) {
  190.                         visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
  191.                     }
  192.                     RotationTimer = 0;
  193.                 //  anim.SetBool ("Run", true);
  194.                 }
  195.             }
  196.             if (MyDirection == Direction.Right || MyDirection == Direction.Left)
  197.             {
  198.                 if (dirNumber==1&&CheckCurrentMove (transform.forward) == true)
  199.                 {
  200.                     MyDirection = Direction.Forward;   
  201.                     nextDest = currentPos + new Vector3 (0, 0, 1);
  202.                     if (Settings.RotatePlayer==false) {
  203.                         visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
  204.                     }
  205.                     RotationTimer = 0;
  206.                 //  anim.SetBool ("Run", true);
  207.                 }
  208.                 if (dirNumber==2&&CheckCurrentMove (-transform.forward) == true)
  209.                 {
  210.                     MyDirection = Direction.Back;  
  211.                     nextDest = currentPos + new Vector3 (0, 0, -1);
  212.                     if (Settings.RotatePlayer==false) {
  213.                         visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
  214.                     }
  215.                     RotationTimer = 0;
  216.                 //  anim.SetBool ("Run", true);
  217.                 }
  218.             }
  219.         }
  220.     }
  221.  
  222.  
  223.  
  224.  
  225.  
  226.     void Update ()
  227.     {
  228.         if (rotationDelay > 0)
  229.         {
  230.             rotationDelay -= Time.deltaTime;
  231.         }
  232.  
  233.         RotationTimer += Time.deltaTime;
  234.         //Forward
  235.         if (Input.GetKeyDown (KeyCode.W)||Input.GetKeyDown(KeyCode.UpArrow))
  236.         {
  237.             MoveForward ();
  238.         }
  239.         if (MyDirection==Direction.Forward)
  240.         {
  241.             if (CheckNextMove (transform.forward) == true)
  242.             {
  243.                 if (currentPos == nextDest)
  244.                 {
  245.                     if (Settings.RotatePlayer==false) {
  246.                         visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
  247.                     }
  248.                     nextDest = currentPos + new Vector3 (0, 0, 1);
  249.                     RotationTimer = 0;
  250.                 }
  251.             }
  252.             else
  253.             {
  254.                 disableRunAnimState = true;
  255.             //  anim.SetBool ("Run", false);   
  256.             }
  257.         }
  258.         //Back
  259.         if (Input.GetKeyDown (KeyCode.S)||Input.GetKeyDown(KeyCode.DownArrow))
  260.         {
  261.             MoveBack ();
  262.         }
  263.         if (MyDirection==Direction.Back)
  264.         {
  265.             if (CheckNextMove (-transform.forward) == true)
  266.             {
  267.                 if (currentPos == nextDest)
  268.                 {
  269.                     if (Settings.RotatePlayer==false) {
  270.                         visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
  271.                     }
  272.                     nextDest = currentPos + new Vector3 (0, 0, -1);
  273.                     RotationTimer = 0;
  274.                 }
  275.             }
  276.             else
  277.             {
  278.                 disableRunAnimState = true;
  279.                 //anim.SetBool ("Run", false); 
  280.             }
  281.         }
  282.         //Right
  283.         if (Input.GetKeyDown (KeyCode.D)||Input.GetKeyDown(KeyCode.RightArrow))
  284.         {
  285.             MoveRight ();
  286.         }
  287.         if (MyDirection==Direction.Right)
  288.         {
  289.             if (CheckNextMove (transform.right) == true)
  290.             {
  291.                 if (currentPos == nextDest)
  292.                 {
  293.                     if (Settings.RotatePlayer==false) {
  294.                         visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
  295.                     }
  296.                     nextDest = currentPos + new Vector3 (1, 0, 0);
  297.                     RotationTimer = 0;
  298.                 }
  299.             }
  300.             else
  301.             {
  302.                 disableRunAnimState = true;
  303.             //  anim.SetBool ("Run", false);   
  304.             }
  305.         }
  306.         //Left
  307.         if (Input.GetKeyDown (KeyCode.A)||Input.GetKeyDown(KeyCode.LeftArrow))
  308.         {
  309.             MoveLeft ();
  310.         }
  311.         if (MyDirection==Direction.Left)
  312.         {
  313.             if (CheckNextMove (-transform.right) == true)
  314.             {
  315.                 if (currentPos == nextDest)
  316.                 {
  317.                     if (Settings.RotatePlayer==false) {
  318.                         visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
  319.                     }
  320.                     nextDest = currentPos + new Vector3 (-1, 0, 0);
  321.                     RotationTimer = 0;
  322.                 }
  323.             }
  324.             else
  325.             {
  326.                 disableRunAnimState = true;
  327.             //  anim.SetBool ("Run", false);   
  328.             }
  329.         }
  330.  
  331.         //Поворот даже если игрок опоздал немного со свайпом (только для пк)
  332.         /*
  333.         if (RotationTimer < 1f)
  334.         {
  335.             if (MyDirection == Direction.Forward || MyDirection == Direction.Back)
  336.             {
  337.                 if (Input.GetKeyDown (KeyCode.D)&&CheckCurrentMove (transform.right) == true)
  338.                 {
  339.                     Debug.Log ("rotate");
  340.                         MyDirection = Direction.Right; 
  341.                     nextDest = currentPos + new Vector3 (1, 0, 0);
  342.                         RotationTimer = 0;
  343.                 }
  344.                 if (Input.GetKeyDown (KeyCode.A)&&CheckCurrentMove (-transform.right) == true)
  345.                 {
  346.                     Debug.Log ("rotate");
  347.                     MyDirection = Direction.Left;  
  348.                     nextDest = currentPos + new Vector3 (-1, 0, 0);
  349.                     RotationTimer = 0;
  350.                 }
  351.             }
  352.             if (MyDirection == Direction.Right || MyDirection == Direction.Left)
  353.             {
  354.                 if (Input.GetKeyDown (KeyCode.W)&&CheckCurrentMove (transform.forward) == true)
  355.                 {
  356.                     Debug.Log ("rotate");
  357.                     MyDirection = Direction.Forward;   
  358.                     nextDest = currentPos + new Vector3 (0, 0, 1);
  359.                     RotationTimer = 0;
  360.                 }
  361.                 if (Input.GetKeyDown (KeyCode.S)&&CheckCurrentMove (-transform.forward) == true)
  362.                 {
  363.                     Debug.Log ("rotate");
  364.                     MyDirection = Direction.Back;  
  365.                     nextDest = currentPos + new Vector3 (0, 0, -1);
  366.                     RotationTimer = 0;
  367.                 }
  368.             }
  369.         }*/
  370.  
  371.  
  372.  
  373.         transform.position = Vector3.MoveTowards (transform.position, nextDest, Speed * Time.deltaTime);
  374. /*
  375.         movementtimer+=Time.deltaTime;
  376.         if (movementtimer > 1 / Speed || changedir) { //0.5f
  377.             changedir = false;
  378.             transform.position = nextDest;
  379.             movementtimer = 0;
  380.         }
  381. */
  382.         if(transform.position==nextDest)
  383.         {
  384.             if (Teleport != 0)
  385.             {
  386.                 Vector3 pos = transform.position;
  387.                 pos.x += Teleport;
  388.                 transform.position = pos;
  389.                 Vector3 pos2 = anim.transform.parent.position;
  390.                 pos2.x += Teleport;
  391.                 anim.transform.parent.position = pos2;
  392.                 Vector3 pos3 = Cam.position;
  393.                 pos3.x += Teleport;
  394.                 Cam.GetComponent<CameraFollow> ().curpos.x += Teleport;
  395.                 Cam.position = pos3;
  396.  
  397.                 Teleport = 0;
  398.                 nextDest = transform.position;
  399.             }
  400.             steptime = 0;
  401.             lastPos = currentPos;
  402.             currentPos = nextDest;
  403.  
  404.             //Для квеста
  405.             ForwardSteps = transform.position.z + 2.5f;
  406.  
  407.  
  408.             if (disableRunAnimState) {
  409.                 anim.SetBool ("Run", false);   
  410.             }
  411.         }
  412.  
  413.         visibleTrEnemy.position = Vector3.MoveTowards (visibleTrEnemy.position, transform.position, Time.deltaTime * Speed);
  414.  
  415.  
  416.         if (Settings.RotatePlayer) {
  417.             {
  418.                 Vector3 vector = visibleTrEnemy.position - currentPos;
  419.                 if (vector != Vector3.zero) {
  420.                     var targetRotation = Quaternion.LookRotation (vector, Vector3.up);
  421.  
  422.                 if (rotationDelay > 0)
  423.                 {
  424.                     visibleTrEnemy.rotation = Quaternion.Lerp (visibleTrEnemy.rotation, Quaternion.LookRotation (vector), 25 * Time.deltaTime);
  425.                 }
  426.                 else
  427.                 {
  428.                     visibleTrEnemy.rotation = Quaternion.Lerp (visibleTrEnemy.rotation, Quaternion.LookRotation (vector), Time.deltaTime * rotatePlayerSpeed);
  429.                     }
  430.                 }
  431.             }
  432.         }
  433.     }
  434.  
  435.  
  436.     public bool CheckNextMove(Vector3 destination)
  437.     {
  438.         RaycastHit hit;
  439.         //if (Physics.Raycast (transform.position, destination, out hit, 1))
  440.  
  441.         Vector3 RayStartPos = nextDest + destination + new Vector3 (0,1,0);
  442.         Debug.DrawRay(RayStartPos, -Vector3.up*3);
  443.         if (Physics.Raycast (RayStartPos, -Vector3.up, out hit, 3,rayMask))
  444.         {
  445.             if (hit.transform.CompareTag ("Obstacle"))
  446.             {
  447.                 return false;
  448.             } else
  449.             {
  450.                 return true;
  451.             }
  452.         } else
  453.         {
  454.             return false;
  455.         }
  456.         return true;
  457.     }
  458.  
  459.     public bool CheckCurrentMove(Vector3 destination)
  460.     {
  461.         RaycastHit hit;
  462.         //if (Physics.Raycast (transform.position, destination, out hit, 1))
  463.  
  464.         Vector3 RayStartPos = currentPos + destination+ new Vector3 (0,1,0);
  465.  
  466.         if (Physics.Raycast (RayStartPos, -Vector3.up, out hit, 3, rayMask)) {
  467.             if (hit.transform.CompareTag ("Obstacle")) {
  468.                 return false;
  469.             } else {
  470.                 return true;
  471.             }
  472.         } else
  473.         {
  474.             return false;
  475.         }
  476.         return true;
  477.  
  478.     }
  479.  
  480.  
  481.     public void RotateSpeedControlViaSettings(float rotSpeed)
  482.     {
  483.         rotatePlayerSpeed = rotSpeed;
  484.         rotSpeedText.text = rotatePlayerSpeed.ToString ("F0");
  485.     }
  486.  
  487.     public void OnDrawGizmos()
  488.     {
  489.     //  Gizmos.DrawSphere (nextDest + transform.forward + new Vector3 (0,1,0),0.5f);
  490.     }
  491. }
Add Comment
Please, Sign In to add comment