void Update() { // Fire a raycast from the bullet's current position to its next position Vector3 nextPosition = transform.position + (transform.forward * bulletSpeed * Time.deltaTime); RaycastHit hit; if (Collision.CanHit(transform.position, nextPosition, out hit)) { // Check if the hit object is an enemy and apply damage if it is Enemy enemy = hit.collider.GetComponent(); if (enemy != null) { enemy.TakeDamage(bulletDamage); } // Destroy the bullet Destroy(gameObject); } }
void Update() { // Fire a raycast from the player's current position to the ground ahead Vector3 nextPosition = transform.position + (transform.forward * jumpDistance); RaycastHit hit; if (Collision.CanHit(transform.position, nextPosition, out hit)) { // Check if the hit object is too high to jump over if (hit.distance > jumpHeight) { canJump = false; } else { canJump = true; } } }This example uses Collision.CanHit to check if a player can jump over an obstacle in front of them. If the raycast hits an object that is higher than the player's jump height, the player is unable to jump over it.