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 PlayerController : MonoBehaviour
- {
- public float speed = 100.0f;
- public float jumpForce = 350.0f;
- public float airDrag = 0.8f;
- private Rigidbody2D body;
- private SpriteRenderer spriteRenderer;
- public Animator animator;
- private UnityEngine.Experimental.Rendering.Universal.Light2D headLight;
- PlayerHealth player;
- private Vector2 currentVelocity;
- private float previousPositionY;
- public Grid background;
- private GameObject soundPlayer;
- public bool isGrounded = true;
- public bool notFloating = true;
- public int yVelocity;
- public int xVelocity;
- private bool groundCheck = true;
- private bool jumpOk;
- public bool invincibilityTimer = false;
- private bool running = false;
- private bool isDead = false;
- public bool startDying = false;
- // Start is called before the first frame update
- void Start()
- {
- body = GetComponent<Rigidbody2D>();
- spriteRenderer = GetComponent<SpriteRenderer>();
- headLight = GetComponentInChildren<UnityEngine.Experimental.Rendering.Universal.Light2D>();
- player = GetComponent<PlayerHealth>();
- soundPlayer = gameObject;
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- jumpOk = true;
- }
- if (Time.timeScale == 0)
- {
- jumpOk = false;
- }
- // tsekkaa vielä ehkä parempi?
- if (animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Run") && !running)
- {
- soundPlayer.SendMessage("PlayFootsteps");
- running = true;
- }
- else if(!animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Run") && running)
- {
- soundPlayer.SendMessage("StopFootsteps");
- running = false;
- }
- if(player.currentHealth == 0)
- {
- isDead = true;
- if (animator.GetCurrentAnimatorStateInfo(0).IsName("Animation_Die"))
- {
- startDying = true;
- }
- }
- }
- void OnCollisionStay2D(Collision2D collider)
- {
- if (collider.gameObject.CompareTag("Ground") || collider.gameObject.CompareTag("Elevator"))
- {
- int i = 0;
- foreach (ContactPoint2D contact in collider.contacts)
- {
- if (0.15f > (body.transform.position.y - contact.point.y) && (body.transform.position.y - contact.point.y) > 0.147f)
- {
- isGrounded = true;
- }
- i++;
- }
- }
- }
- private void OnTriggerEnter2D(Collider2D other)
- {
- if (other.gameObject.CompareTag("Hazard") && !invincibilityTimer)
- {
- soundPlayer.SendMessage("PlayOj");
- player.ChangeHealth(-40);
- invincibilityTimer = true;
- Invoke("ResetInvulnerability", 2);
- }
- }
- private void FixedUpdate()
- {
- if (Time.timeScale > 0)
- Move();
- }
- private void Move()
- {
- bool isJumping = false;
- float velocity = Input.GetAxis("Horizontal") * speed;
- if (jumpOk)
- {
- isJumping = Input.GetKey(KeyCode.Space);
- }
- xVelocity = Mathf.RoundToInt(body.velocity.x);
- if (!isGrounded)
- {
- velocity *= airDrag;
- }
- // Horizontal Movement
- if(!isDead)
- body.velocity = Vector2.SmoothDamp(body.velocity, new Vector2(velocity, body.velocity.y), ref currentVelocity, 0.02f);
- // Initiate Jump
- yVelocity = Mathf.RoundToInt(body.velocity.y);
- if (yVelocity < 0 || yVelocity == 0 && xVelocity != 0)
- {
- notFloating = true;
- }
- // Initiate Jump
- if (isGrounded && yVelocity == 0 && notFloating == true)
- {
- groundCheck = true;
- if (isJumping && !isDead)
- {
- body.AddForce(new Vector2(0, jumpForce));
- soundPlayer.SendMessage("PlayUmmph");
- isGrounded = false;
- }
- }
- else
- {
- groundCheck = false;
- }
- if (yVelocity > 0)
- {
- notFloating = false;
- }
- // Cancel Jump
- if (!isJumping && body.velocity.y > 0.01f)
- {
- body.velocity = new Vector2(body.velocity.x, body.velocity.y * 0.95f);
- }
- if (velocity < 0 && spriteRenderer.flipX == false && !isDead)
- {
- spriteRenderer.flipX = true;
- headLight.transform.rotation = Quaternion.Inverse(headLight.transform.rotation);
- }
- else if (velocity > 0 && spriteRenderer.flipX == true && !isDead)
- {
- spriteRenderer.flipX = false;
- headLight.transform.rotation = Quaternion.Inverse(headLight.transform.rotation);
- }
- animator.SetFloat("VelocityY", Mathf.RoundToInt(body.velocity.y));
- animator.SetFloat("VelocityX", Mathf.Abs(body.velocity.x));
- animator.SetBool("IsGrounded", groundCheck);
- animator.SetBool("gameIsOver", isDead);
- }
- void ResetInvulnerability()
- {
- invincibilityTimer = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement