Raycast on left side and right side of the player
Hi ! In order to move my player in my game I use raycasting. I succeed in check if there is a cube in front of, below and in front down direction of my sphere. But I have a lot of trouble for checking left side and right side of my player.
This is the part of code I use to guide the ray:
void calculRay()
{
rayFrontDirection = transform.right;
rayDownDirection = -transform.up;
rayFrontDownDirection = transform.right - transform.up * 0.5f;
}
I would like to raycast on a LeftDownDirection and a RightDownDirection, have you got any tips please ?
Thank you for you help !
Answer by ashutosh999 · Mar 15, 2021 at 05:16 PM
hey, I guess you can Raycast from the player's collider's position and for direction maybe you can use Vector3.one and use a distance of Mathf. infinity so you have infinitely long ray in all directions.
private BoxCollider _playerBoxCollider;
void Update()
{
RaycastHit hit;
if (Physics.Raycast(_playerBoxCollider.bounds.center, Vector3.one, out hit, Mathf.Infinity)
{
if (hit.collide != null)
{
// your check/logic here
}
}
}
Your answer
Follow this Question
Related Questions
Raycast bug is making me go nnuts,Raycast bug is making me go nuts 0 Answers
RayCast Rotation 1 Answer
raycast bug when hitting two adjacent objects 0 Answers
Check if position is inside a collider 5 Answers