quhluh

C# Backflip Mechanic

Feb 10th, 2024
1,287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.62 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class BackflipController : MonoBehaviour
  4. {
  5.     public float backflipForce = 500f;
  6.     public float backflipRotationForce = 100f;
  7.  
  8.     private Rigidbody rb;
  9.  
  10.     void Start()
  11.     {
  12.         rb = GetComponent<Rigidbody>();
  13.     }
  14.  
  15.     void Update()
  16.     {
  17.         if (Input.GetKeyDown(KeyCode.Space))
  18.         {
  19.             PerformBackflip();
  20.         }
  21.     }
  22.  
  23.     void PerformBackflip()
  24.     {
  25.         // Apply force upwards
  26.         rb.AddForce(Vector3.up * backflipForce);
  27.  
  28.         // Apply torque for rotation
  29.         rb.AddTorque(transform.right * backflipRotationForce);
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment