Advertisement
sergezhu

Untitled

Dec 6th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. [RequireComponent(typeof(Rigidbody))]
  2. public class BallJumper : MonoBehaviour
  3. {
  4.     [SerializeField] private float _jumpForce;
  5.  
  6.     private Rigidbody _rigidbody;
  7.     private bool _isCollision = false;
  8.     private bool _isFinished = false;
  9.      
  10.  
  11.     private void Awake()
  12.     {
  13.         _rigidbody = GetComponent<Rigidbody>();
  14.     }
  15.  
  16.     private void OnCollisionEnter(Collision collision)
  17.     {
  18.         if (_isCollision || _isFinished) return;
  19.        
  20.         if (collision.gameObject.TryGetComponent<PlatformSegment>(out PlatformSegment platformSegment))
  21.         {
  22.             if(platformSegment.GetComponentInParent<FinishPlatform>() != null)
  23.             {
  24.                 Debug.Log("Ball is finished!");
  25.                 _isFinished = true;
  26.             }
  27.             else
  28.             {
  29.                 _rigidbody.velocity = Vector3.zero;
  30.                 _rigidbody.AddForce(Vector3.up * _jumpForce, ForceMode.Impulse);
  31.                 _isCollision = true;
  32.             }                        
  33.         }
  34.     }
  35.  
  36.     private void FixedUpdate()
  37.     {
  38.         _isCollision = false;
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement