unity3d Raycast Physics Raycast

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

This function casts a ray from point origin in direction direction of length maxDistance against all colliders in the scene.

The function takes in the origin direction maxDistance and calculate if there is a collider in front of the GameObject.

Physics.Raycast(origin, direction, maxDistance);

For example, this function will print Hello World to the console if there is something within 10 units of the GameObject attached to it:

using UnityEngine;

public class TestPhysicsRaycast: MonoBehaviour 
{
    void FixedUpdate() 
    {
        Vector3 fwd = transform.TransformDirection(Vector3.forward);
        
        if (Physics.Raycast(transform.position, fwd, 10)) 
            print("Hello World");
    }
}


Got any unity3d Question?