Advertisement
LeeMace

Player jump double jump double speed

Dec 2nd, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour
  6. {
  7.     private Rigidbody playerRB;
  8.     private Animator playerAnim;
  9.  
  10.     public ParticleSystem explosionParticle;
  11.     public ParticleSystem dirtParticle;
  12.  
  13.     public float jumpForce = 10;
  14.     public float gravityModifier;
  15.  
  16.     public float doubleJumpForce;
  17.     public bool doubleJumpUsed = false;
  18.     public bool doubleSpeed = false;
  19.  
  20.     public bool isOnGround;
  21.     public bool gameOver = false;
  22.  
  23.     public AudioClip jumpSound;
  24.     public AudioClip crashSound;
  25.     private AudioSource playerAudio;
  26.  
  27.     // Start is called before the first frame update
  28.     void Start()
  29.     {
  30.         playerRB = GetComponent<Rigidbody>();
  31.         Physics.gravity *= gravityModifier;
  32.         playerAnim = GetComponent<Animator>();
  33.         playerAudio = GetComponent<AudioSource>();
  34.     }
  35.  
  36.     // Update is called once per frame
  37.     void Update()
  38.     {// press space to make player jump but not to jump when in the air and not jump when dead
  39.         // and dirt stops when jumping
  40.         if (Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
  41.         {
  42.             playerRB.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
  43.             isOnGround = false;
  44.             playerAnim.SetTrigger("Jump_trig");
  45.             dirtParticle.Stop();
  46.             playerAudio.PlayOneShot(jumpSound, 1.0f);
  47.             //double jump (to line 49)
  48.             doubleJumpUsed = false;
  49.         }
  50.         else if(Input.GetKeyDown(KeyCode.Space) && !isOnGround && !doubleJumpUsed)
  51.         {
  52.             doubleJumpUsed = true;
  53.             playerRB.AddForce(Vector3.up * doubleJumpForce, ForceMode.Impulse);
  54.             playerAnim.Play("Running_Jump", 3, 0f);
  55.             playerAudio.PlayOneShot(jumpSound, 1.0f);
  56.         }
  57.  
  58.         if(Input.GetKey(KeyCode.LeftShift) && isOnGround)
  59.         {
  60.             doubleSpeed = true;
  61.             playerAnim.SetFloat("Speed_Multiplier", 2.0f);
  62.         }
  63.         else if (doubleSpeed)
  64.         {
  65.             doubleSpeed = false;
  66.             playerAnim.SetFloat("Speed_Multiplier", 1.0f);
  67.         }
  68.     }
  69.     //when player collides with the ground is on ground is true and dirt is kicked up
  70.     private void OnCollisionEnter(Collision collision)
  71.     {
  72.         if (collision.gameObject.CompareTag("Ground"))
  73.         {
  74.         isOnGround = true;
  75.             dirtParticle.Play();
  76.  
  77.         } //if player hits obstacle then game over and death animation plays and he cant jump
  78.         // and dirt stops coming out of his feet
  79.         else if (collision.gameObject.CompareTag("Obstacle"))
  80.         {
  81.             gameOver = true;
  82.             Debug.Log("Game Over");
  83.             playerAnim.SetBool("Death_b", true);
  84.             playerAnim.SetInteger("DeathType_int", 1);
  85.             explosionParticle.Play();
  86.             dirtParticle.Stop();
  87.             playerAudio.PlayOneShot(crashSound, 1.0f);
  88.  
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement