Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [RequireComponent(typeof(Rigidbody))]
- public class BallJumper : MonoBehaviour
- {
- [SerializeField] private float _jumpForce;
- private Rigidbody _rigidbody;
- private bool _isCollision = false;
- private bool _isFinished = false;
- private void Awake()
- {
- _rigidbody = GetComponent<Rigidbody>();
- }
- private void OnCollisionEnter(Collision collision)
- {
- if (_isCollision || _isFinished) return;
- if (collision.gameObject.TryGetComponent<PlatformSegment>(out PlatformSegment platformSegment))
- {
- if(platformSegment.GetComponentInParent<FinishPlatform>() != null)
- {
- Debug.Log("Ball is finished!");
- _isFinished = true;
- }
- else
- {
- _rigidbody.velocity = Vector3.zero;
- _rigidbody.AddForce(Vector3.up * _jumpForce, ForceMode.Impulse);
- _isCollision = true;
- }
- }
- }
- private void FixedUpdate()
- {
- _isCollision = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement