Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- public class PlayerMovement : MonoBehaviour
- {
- public static PlayerMovement instance;
- public Camera playerCamera;
- public Animator myAnimator;
- public GameObject particleFXDead;
- public GameObject particleFX;
- public GameObject body;
- public Transform[] rays = new Transform[3];
- public float speed = 12.0F;
- public float jumpSpeed = 8.0F;
- public float gravity = 20.0F;
- private Vector3 moveDirection = Vector3.zero;
- public bool tapControll = true;
- private bool run = false;
- private Rigidbody rBody;
- private BoxCollider collider;
- void Start()
- {
- instance = this;
- if (playerCamera == null)
- {
- playerCamera = Camera.main;
- }
- playerCamera.transparencySortMode = TransparencySortMode.Orthographic;
- myAnimator.SetTrigger("Start");
- playerCamera.transform.position = new Vector3(transform.position.x + 5, playerCamera.transform.position.y, playerCamera.transform.position.z);
- rBody = GetComponent<Rigidbody>();
- collider = GetComponent<BoxCollider>();
- rBody.freezeRotation = true;
- rBody.useGravity = false;
- }
- bool IsGrounded()
- {
- if (Physics.gravity.y < 0)
- return Physics.Raycast(transform.position, -Vector3.up, collider.bounds.extents.y + 0.1f);
- else return
- Physics.Raycast(transform.position, Vector3.up, collider.bounds.extents.y + 0.1f);
- }
- void FixedUpdate()
- {
- if (!run)
- return;
- moveDirection = new Vector3(1, 0, 0);
- moveDirection = transform.TransformDirection(moveDirection);
- moveDirection *= speed;
- rBody.MovePosition(transform.position + moveDirection * Time.deltaTime);
- if (Input.GetButton("Jump") && IsGrounded())
- {
- rBody.velocity = new Vector3(0, jumpSpeed, 0);
- if (0 < gravity)
- myAnimator.SetTrigger("JumpFloor");
- else
- myAnimator.SetTrigger("JumpCeiling");
- }
- rBody.AddForce(new Vector3(0, -gravity * rBody.mass, 0));
- playerCamera.transform.position = new Vector3(transform.position.x + 5, playerCamera.transform.position.y, playerCamera.transform.position.z);
- for (int i = 0; i < 3; i++)
- {
- if (Physics.Raycast(rays[i].position, Vector3.right, collider.bounds.extents.y + 0.1f, ~( 1 << 11))) Collided();
- }
- }
- public void Collided()
- {
- body.SetActive(false);
- particleFX.SetActive(false);
- particleFXDead.SetActive(true);
- run = false;
- rBody.isKinematic = true;
- SoundManager.instance.StopLevelSound();
- return;
- }
- void OnTriggerEnter(Collider other)
- {
- if (other.gameObject.tag == "GravityUP")
- {
- gravity *= -1;
- Physics.gravity = new Vector3 (0,50f,0);
- jumpSpeed *= -1;
- return;
- }
- if (other.gameObject.tag == "GravityDOWN")
- {
- gravity *= -1;
- Physics.gravity = new Vector3(0, -50f, 0);
- jumpSpeed *= -1;
- return;
- }
- if (other.gameObject.tag == "Controll")
- {
- if (tapControll)
- tapControll = false;
- else
- tapControll = true;
- return;
- }
- //End Animation
- if (other.gameObject.tag == "LevelEnd")
- {
- if (!run)
- return;
- rBody.useGravity = false;
- run = false;
- LeanTween.move(gameObject, VortexMiddle.instance.gameObject.transform.position, 1.5f).setEase(LeanTweenType.easeInQuad);
- myAnimator.SetTrigger("LevelEnd");
- return;
- }
- if (other.gameObject.tag == "EndPoint")
- {
- particleFX.GetComponent<ParticleSystem>().Stop();
- myAnimator.SetTrigger("End");
- LevelStatsManager.instance.stats.success++;
- LevelStatsManager.instance.stats.beaten = true;
- }
- }
- public void PlayerRun()
- {
- run = true;
- if (0 < Physics.gravity.y) Physics.gravity *= -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment