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 Player : MonoBehaviour {
- //[SerializeField]
- //GameObject TwoDoorRoom;
- private CharacterController controller;
- private Vector3 moveVec;
- //Speed control
- public float speed = 5.0f;
- //private Vector3 gravity;
- //line controll
- public bool isAnyDoorOpened = false;
- private bool isDead = false;
- public float distanseBetweenPlayerAndRoomEnd;
- //___for init all types of rooms and get their lenghs
- public float twoDoorRoomLength;
- //private float ThreeDoorRoomLength;
- private float someZ = 34.0f;
- private GameObject[] threeDoorRooms;
- private GameObject[] twoDoorRooms;
- private GameObject[] OneDoorRooms;
- //suppor for save postion.x when player goes from current room to next room
- private bool isAnyKeyPressed = false;
- public bool isInBeatweenRooms = false;
- public List<string> pushs;
- public Vector3 targetPos;
- private Swiper swiper;
- void Start ()
- {
- //twoDoorRoomLength = TwoDoorRoom.GetComponent<Collider>().bounds.size.z;
- swiper = gameObject.GetComponent<Swiper>();
- pushs = new List<string>();
- pushs.Add("EMPTY"); //fill the list cause of avoid error "out of range" when we chek for coincidence in CheckerUserInput
- pushs.Add("EMPTY");
- pushs.Add("EMPTY");
- targetPos = transform.position;
- //___for init all types of rooms and get their lenghs
- // threeDoorRoom = GameObject.FindGameObjectWithTag("ThreeDoor");
- // twoDoorRoom = GameObject.FindGameObjectWithTag("TwoDoor");
- //ThreeDoorRoomLength = threeDoorRoom.GetComponent<TestRoomManager>().RoomLength();
- }
- void Update ()
- { //print(Mathf.Abs(targetPos.x-transform.position.x));
- //Debug.Log(string.Join("+,", pushs.ToArray()));
- if(isDead)
- return; //stop update player if he's dead
- if (transform.position.z > someZ + 2.0f)
- { //print("player.transform.position.z > someZ");
- isAnyDoorOpened = false;
- isInBeatweenRooms = true;
- someZ += twoDoorRoomLength;
- }
- else isInBeatweenRooms = false;
- //print(isAnyDoorOpened);
- //Debug.Log(twoDoorRoomLength);
- if(transform.position.z > (twoDoorRoomLength / 2) && isAnyDoorOpened == false) //restrick moving in first room and while door is opened
- {
- #region ADD PUSHS TO A LIST
- //_______________________________ADD PUSHS TO A LIST__________________________
- if(Input.GetKeyDown(KeyCode.UpArrow) || swiper.SwipeUp)
- { print("swipeUp");
- pushs.Add("Up");
- //Debug.Log(string.Join("+,", pushs.ToArray()));
- }
- if (Input.GetKeyDown(KeyCode.DownArrow) || swiper.SwipeDown)
- {
- pushs.Add("Down");
- //Debug.Log(string.Join("+,", pushs.ToArray()));
- }
- if (pushs.Count > 2)
- {
- pushs.RemoveAt(0);
- }
- //Debug.Log(string.Join("+,", pushs.ToArray()));
- //_______________________________________________________________________
- #endregion
- //set left and right boarders to move
- SetTarggetPosition();
- MoveObject();
- }
- if (isInBeatweenRooms == true || (isAnyDoorOpened == false && Input.GetKeyDown(KeyCode.LeftArrow)) || (isAnyDoorOpened == false && Input.GetKeyDown(KeyCode.RightArrow)))
- {
- pushs.Add("EMPTY"); //fill the list cause of avoid error "out of range" when we chek for coincidence in CheckerUserInput
- pushs.Add("EMPTY");
- while (pushs.Count > 2)
- {
- pushs.RemoveAt(0);
- }
- }
- else if (pushs.Count < 2)
- {
- pushs.Add("EMPTY"); //fill the list cause of avoid error "out of range" when we chek for coincidence in CheckerUserInput
- pushs.Add("EMPTY");
- while (pushs.Count > 2)
- {
- pushs.RemoveAt(0);
- }
- }
- //Move ball forward
- transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + speed*Time.deltaTime);
- //Set score
- if(transform.position.z > 34f)
- {
- GetComponent<Score>().score = transform.position.z - 34f;
- }
- }
- //Player movement with mouse and touches
- void SetTarggetPosition()
- {
- Plane plane = new Plane(Vector3.up,transform.position);
- //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //MOUSE
- Ray ray2 = Camera.main.ScreenPointToRay(Input.touches[0].position); //TOUCH
- float point = 0f;
- // if(plane.Raycast(ray, out point)) //MOUSE
- // targetPos = ray.GetPoint(point);
- if(plane.Raycast(ray2, out point)) //TOUCH
- targetPos = ray2.GetPoint(point);
- }
- void MoveObject()
- {
- float xPos;
- if(Input.GetMouseButton(0))
- {
- xPos = Mathf.Lerp(transform.position.x, targetPos.x, 15 * Time.deltaTime);
- }
- else xPos = transform.position.x;
- transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
- }
- //________________________________________
- private void PlayeStop()
- {
- moveVec.z = 0;
- }
- //____________________________________________DEATH CONDITION____________________________________
- private void OnControllerColliderHit(ControllerColliderHit hit)
- {
- // if (hit.point.z > transform.position.z + controller.radius || hit.point.x > transform.position.x - 0.2f)
- // { print(hit.point.z);
- // print(transform.position.z + controller.radius);
- // print(hit.point.x);
- // print(transform.position.x - 0.2f);
- // Death();
- // }
- // //Death();
- }
- void OnCollisionEnter(Collision col)
- {
- Death();
- }
- private void Death()
- {
- isDead = true;
- gameObject.GetComponent<Score>().OnDeath();
- Debug.Log("DEAD");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement