CTIN 289 - Interactive Snippets Library

Powered by 🌱Roam Garden

Raycast Syntax (3D)

RaycastHit raycastHit;
Physics.Raycast(startofRay, direction, out raycastHit);avascript

//startOfRay = where to start the ray (Vector3)
//direction = which direction it goes (Vector3)
//raycastHit = an out parameter to hold the result (RaycastHit)

//get the GameObject that the ray hit (just like with 2D)
raycastHit.collider.gameObject;

//make ray visible
Debug.DrawRay;
Debug.DrawRay(startPosition, directionAndDistance, color);

//Visualizing Raycasts
Vector3 dirToPlayer = player.transform.position - transform.position;
Debug.DrawRay(transform.position, dirToPlayer, Color.red);

// but dirToPlayer is a vector that points directly to the player. If you want to look in the player's direction, but only a certain distance, you will NORMALIZE the vector

Vector3 dirToPlayer = player.transform.position - transform.position;
Vector3 towardPlayer = dirToPlayer.normalized * distance;

Debug.DrawRay(transform.position, towardPlayer, Color.red);