Advertisement
Guest User

Untitled

a guest
Nov 11th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Player : MonoBehaviour
  6. {
  7.     public Player()
  8.     { }
  9.  
  10.     private bool jumpKeyWasPressed;
  11.     private float horizontalInput;
  12.     private Rigidbody rigidBodyComponent;
  13.     public Transform groundCheckTransform;
  14.     public int superJumpsRemaining;
  15.  
  16.  
  17.  
  18.  
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.         rigidBodyComponent = GetComponent<Rigidbody>();
  23.     }
  24.  
  25.     // Update is called once per frame
  26.     void Update()
  27.     {
  28.         if (Input.GetKeyDown(KeyCode.Space))
  29.         {
  30.             jumpKeyWasPressed = true;
  31.         }
  32.  
  33.         horizontalInput = Input.GetAxis("Horizontal");
  34.     }
  35.     //Fixedupdate wird jedes physikupdate abgerufen (100 mal die sekunde)
  36.     private void FixedUpdate()
  37.     {
  38.        
  39.         GetComponent<Rigidbody>().velocity = new Vector3(horizontalInput, GetComponent<Rigidbody>().velocity.y, 0);
  40.  
  41.         if (Physics.OverlapSphere(groundCheckTransform.position, 0.1f).Length == 1)
  42.         {
  43.             return;
  44.         }
  45.  
  46.        
  47.  
  48.         if (jumpKeyWasPressed == true)
  49.         {
  50.             float jumpPower = 5f;
  51.             if(superJumpsRemaining > 0) {
  52.  
  53.                
  54.                
  55.                
  56.  
  57.                 jumpPower *= 1.5f;
  58.                 superJumpsRemaining--;
  59.             }
  60.             Debug.Log("Space wurde gedrückt");
  61.             GetComponent<Rigidbody>().AddForce(Vector3.up * jumpPower, ForceMode.VelocityChange);
  62.             jumpKeyWasPressed = false;
  63.            
  64.         }
  65.        
  66.     }
  67.     private void OnTriggerEnter(Collider other)
  68.     {
  69.         if (other.gameObject.layer == 9)
  70.         {
  71.             Destroy(other.gameObject);
  72.             superJumpsRemaining++;
  73.         }
  74.     }
  75.  
  76. }
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement