UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"a infinite runner"
using UnityEngine; public class InfiniteRunner : MonoBehaviour { public float speed = 5f; public float jumpForce = 10f; public LayerMask groundLayer; public Transform groundCheck; private Rigidbody rb; private bool isGrounded; // Start is called before the first frame update void Start() { rb = GetComponent<Rigidbody>(); } // Update is called once per frame void Update() { // Move the player forward transform.Translate(Vector3.forward * speed * Time.deltaTime); // Check if the player is grounded isGrounded = Physics.CheckSphere(groundCheck.position, 0.1f, groundLayer); // Jump if the player is grounded and the space key is pressed if (isGrounded && Input.GetKeyDown(KeyCode.Space)) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); } } // OnTriggerEnter is called when the player enters a trigger collider void OnTriggerEnter(Collider other) { // If the player enters a trigger with the tag "Obstacle", restart the level if (other.CompareTag("Obstacle")) { UnityEngine.SceneManagement.SceneManager.LoadScene(UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex); } } }