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
- {
- private Rigidbody playerRB;
- private Animator playerAnim;
- public ParticleSystem explosionParticle;
- public ParticleSystem dirtParticle;
- public float jumpForce = 10;
- public float gravityModifier;
- public float doubleJumpForce;
- public bool doubleJumpUsed = false;
- public bool doubleSpeed = false;
- public bool isOnGround;
- public bool gameOver = false;
- public AudioClip jumpSound;
- public AudioClip crashSound;
- private AudioSource playerAudio;
- // Start is called before the first frame update
- void Start()
- {
- playerRB = GetComponent<Rigidbody>();
- Physics.gravity *= gravityModifier;
- playerAnim = GetComponent<Animator>();
- playerAudio = GetComponent<AudioSource>();
- }
- // Update is called once per frame
- void Update()
- {// press space to make player jump but not to jump when in the air and not jump when dead
- // and dirt stops when jumping
- if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
- {
- playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
- isOnGround = false;
- playerAnim.SetTrigger("Jump_trig");
- dirtParticle.Stop();
- playerAudio.PlayOneShot(jumpSound, 1.0f);
- //double jump (to line 49)
- doubleJumpUsed = false;
- }
- else if(Input.GetKeyDown(KeyCode.Space) && !isOnGround && !doubleJumpUsed)
- {
- doubleJumpUsed = true;
- playerRB.AddForce(Vector3.up * doubleJumpForce, ForceMode.Impulse);
- playerAnim.Play("Running_Jump", 3, 0f);
- playerAudio.PlayOneShot(jumpSound, 1.0f);
- }
- if(Input.GetKey(KeyCode.LeftShift) && isOnGround)
- {
- doubleSpeed = true;
- playerAnim.SetFloat("Speed_Multiplier", 2.0f);
- }
- else if (doubleSpeed)
- {
- doubleSpeed = false;
- playerAnim.SetFloat("Speed_Multiplier", 1.0f);
- }
- }
- //when player collides with the ground is on ground is true and dirt is kicked up
- private void OnCollisionEnter(Collision collision)
- {
- if (collision.gameObject.CompareTag("Ground"))
- {
- isOnGround = true;
- dirtParticle.Play();
- } //if player hits obstacle then game over and death animation plays and he cant jump
- // and dirt stops coming out of his feet
- else if (collision.gameObject.CompareTag("Obstacle"))
- {
- gameOver = true;
- Debug.Log("Game Over");
- playerAnim.SetBool("Death_b", true);
- playerAnim.SetInteger("DeathType_int", 1);
- explosionParticle.Play();
- dirtParticle.Stop();
- playerAudio.PlayOneShot(crashSound, 1.0f);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement