Advertisement
Guest User

player

a guest
Apr 23rd, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.58 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour {
  6.  
  7.     //[SerializeField]
  8.     //GameObject TwoDoorRoom;
  9.     private CharacterController controller;
  10.     private Vector3 moveVec;
  11.     //Speed control
  12.     public float speed = 5.0f;
  13.     //private Vector3 gravity;
  14.     //line controll
  15.    
  16.  
  17.     public bool isAnyDoorOpened = false;   
  18.     private bool isDead = false;
  19.     public float distanseBetweenPlayerAndRoomEnd;  
  20.  
  21.     //___for init all types of rooms and get their lenghs  
  22.     public float twoDoorRoomLength;
  23.     //private float ThreeDoorRoomLength;
  24.     private float someZ = 34.0f;
  25.     private GameObject[] threeDoorRooms;
  26.     private GameObject[] twoDoorRooms;
  27.     private GameObject[] OneDoorRooms;
  28.  
  29.     //suppor for save postion.x when player goes from current room to next room
  30.     private bool isAnyKeyPressed = false;
  31.     public bool isInBeatweenRooms = false;
  32.  
  33.     public List<string> pushs;
  34.     public Vector3 targetPos;
  35.  
  36.     private Swiper swiper;
  37.    
  38.    
  39.  
  40.    
  41.  
  42.     void Start ()
  43.     {
  44.         //twoDoorRoomLength = TwoDoorRoom.GetComponent<Collider>().bounds.size.z;
  45.         swiper = gameObject.GetComponent<Swiper>();
  46.         pushs = new List<string>();
  47.         pushs.Add("EMPTY");        //fill the list cause of avoid error "out of range" when we chek for coincidence in CheckerUserInput
  48.         pushs.Add("EMPTY");
  49.         pushs.Add("EMPTY");    
  50.        
  51.         targetPos = transform.position;
  52.            
  53.  
  54.         //___for init all types of rooms and get their lenghs      
  55.         // threeDoorRoom = GameObject.FindGameObjectWithTag("ThreeDoor");
  56.         // twoDoorRoom = GameObject.FindGameObjectWithTag("TwoDoor");
  57.            
  58.         //ThreeDoorRoomLength = threeDoorRoom.GetComponent<TestRoomManager>().RoomLength();        
  59.     }
  60.  
  61.     void Update ()
  62.     {   //print(Mathf.Abs(targetPos.x-transform.position.x));
  63.         //Debug.Log(string.Join("+,", pushs.ToArray()));
  64.         if(isDead)
  65.             return; //stop update player if he's dead      
  66.  
  67.         if (transform.position.z > someZ + 2.0f)
  68.         {   //print("player.transform.position.z > someZ");
  69.             isAnyDoorOpened = false;
  70.             isInBeatweenRooms = true;
  71.             someZ += twoDoorRoomLength;            
  72.         }  
  73.         else isInBeatweenRooms = false;
  74.        
  75.         //print(isAnyDoorOpened);
  76.         //Debug.Log(twoDoorRoomLength);
  77.         if(transform.position.z > (twoDoorRoomLength / 2)  && isAnyDoorOpened == false) //restrick moving in first room and while door is opened
  78.         {
  79.             #region ADD PUSHS TO A LIST
  80.             //_______________________________ADD PUSHS TO A LIST__________________________
  81.             if(Input.GetKeyDown(KeyCode.UpArrow) || swiper.SwipeUp)
  82.             {   print("swipeUp");
  83.                 pushs.Add("Up");
  84.                 //Debug.Log(string.Join("+,", pushs.ToArray()));
  85.             }
  86.             if (Input.GetKeyDown(KeyCode.DownArrow) || swiper.SwipeDown)
  87.             {
  88.                 pushs.Add("Down");
  89.                 //Debug.Log(string.Join("+,", pushs.ToArray()));               
  90.             }
  91.             if (pushs.Count > 2)
  92.             {
  93.                 pushs.RemoveAt(0);
  94.             }
  95.             //Debug.Log(string.Join("+,", pushs.ToArray()));
  96.             //_______________________________________________________________________
  97.             #endregion
  98.  
  99.  
  100.             //set left and right boarders to move
  101.            
  102.             SetTarggetPosition();
  103.             MoveObject();  
  104.                    
  105.         }
  106.  
  107.  
  108.         if (isInBeatweenRooms == true || (isAnyDoorOpened == false && Input.GetKeyDown(KeyCode.LeftArrow)) || (isAnyDoorOpened == false && Input.GetKeyDown(KeyCode.RightArrow)))
  109.         {
  110.             pushs.Add("EMPTY");     //fill the list cause of avoid error "out of range" when we chek for coincidence in CheckerUserInput
  111.             pushs.Add("EMPTY");
  112.             while (pushs.Count > 2)
  113.             {
  114.                 pushs.RemoveAt(0);
  115.             }      
  116.         }
  117.         else if (pushs.Count < 2)
  118.         {
  119.             pushs.Add("EMPTY");     //fill the list cause of avoid error "out of range" when we chek for coincidence in CheckerUserInput
  120.             pushs.Add("EMPTY");
  121.             while (pushs.Count > 2)
  122.             {
  123.                 pushs.RemoveAt(0);
  124.             }
  125.         }
  126.        
  127.  
  128.         //Move ball forward
  129.         transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + speed*Time.deltaTime);
  130.         //Set score
  131.         if(transform.position.z > 34f)
  132.         {
  133.             GetComponent<Score>().score = transform.position.z - 34f;
  134.         }
  135.        
  136.     }  
  137.     //Player movement with mouse and touches
  138.     void SetTarggetPosition()
  139.     {
  140.         Plane plane = new Plane(Vector3.up,transform.position);
  141.         //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //MOUSE
  142.         Ray ray2 = Camera.main.ScreenPointToRay(Input.touches[0].position); //TOUCH
  143.         float point = 0f;
  144.  
  145.         // if(plane.Raycast(ray, out point))   //MOUSE
  146.         //     targetPos = ray.GetPoint(point);  
  147.         if(plane.Raycast(ray2, out point))  //TOUCH
  148.             targetPos = ray2.GetPoint(point);                
  149.          
  150.     }
  151.     void MoveObject()
  152.     {  
  153.         float xPos;
  154.         if(Input.GetMouseButton(0))
  155.         {
  156.             xPos =  Mathf.Lerp(transform.position.x, targetPos.x, 15 * Time.deltaTime);  
  157.         }
  158.         else xPos = transform.position.x;
  159.         transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
  160.     }
  161.     //________________________________________
  162.  
  163.    
  164.  
  165.    
  166.     private void PlayeStop()
  167.     {
  168.         moveVec.z = 0;
  169.     }
  170.  
  171.     //____________________________________________DEATH CONDITION____________________________________
  172.     private void OnControllerColliderHit(ControllerColliderHit hit)
  173.     {
  174.         // if (hit.point.z > transform.position.z + controller.radius || hit.point.x > transform.position.x - 0.2f)
  175.         // {    print(hit.point.z);
  176.         //  print(transform.position.z + controller.radius);
  177.         //  print(hit.point.x);
  178.         //  print(transform.position.x - 0.2f);
  179.         //  Death();           
  180.         // }
  181.         // //Death();
  182.        
  183.     }
  184.     void OnCollisionEnter(Collision col)
  185.     {        
  186.         Death();
  187.     }
  188.    
  189.  
  190.     private void Death()
  191.     {
  192.         isDead = true;
  193.         gameObject.GetComponent<Score>().OnDeath();
  194.         Debug.Log("DEAD");
  195.     }
  196.  
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement