Untitled

Anonymous
plain_text
01/23/2026 2:48 AM
3.2 KB
8
Indexable
// Assets/Scripts/PlayerJump.cs

using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class PlayerJump : MonoBehaviour
{
        [Header("Jump Settings")]
            public float jumpForce = 7f;
                public LayerMask groundLayer;

                    private Rigidbody rb;
                        private bool isGrounded;

                            private void Awake()
                                {
                                            rb = GetComponent<Rigidbody>();
                                }

                                    private void Update()
                                        {
                                                    if (Input.GetButtonDown("Jump") && isGrounded)
                                                            {
                                                                            Jump();
                                                            }
                                        }

                                            private void Jump()
                                                {
                                                            rb.velocity = new Vector3(rb.velocity.x, 0f, rb.velocity.z);
                                                                    rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
                                                                            isGrounded = false;
                                                }

                                                    private void OnCollisionEnter(Collision collision)
                                                        {
                                                                    if (((1 << collision.gameObject.layer) & groundLayer) != 0)
                                                                            {
                                                                                            isGrounded = true;
                                                                            }
                                                        }
}
                                                                            }
                                                        }
                                                }
                                                            }
                                        }
                                }
}
Editor is loading...
Leave a Comment