- Home /
Can anyone please tell me why this script doesnt work? - Raycasting
Hi,
I have 3 rays set up to look in the left-diagonal, forward and right-diagonal directions. The forward and right-diagonal are working (detecting objects) but the left-diagonal is not.
I was wondering if anyone can shed any light on what the issue could be? The script is shown below:
Thanks
{
private Vector3 fwd = Vector3.zero;
private Vector3 diagonal_left = Vector3.zero;
private Vector3 diagonal_right = Vector3.zero;
void Start ()
{
fwd = transform.TransformDirection(Vector3.forward);
diagonal_left = new Vector3(-1f, 0f, 1f).normalized;
diagonal_right = new Vector3(1f, 0f, 1f).normalized;
}
void Update ()
{
RaycastHit hit;
Debug.DrawRay(transform.position, fwd * 500, Color.green);
if(Physics.Raycast(transform.position, fwd, out hit, 100) )
{
Debug.Log ("Front Hit!");
Debug.DrawRay(transform.position, fwd * 500, Color.red);
}
Debug.DrawRay(transform.position, diagonal_left * 100, Color.green);
if(Physics.Raycast(transform.position, diagonal_left, out hit, 100) )
{
Debug.Log ("Left Diagonal Hit!");
Debug.DrawRay(transform.position, diagonal_left * 100, Color.red);
}
Debug.DrawRay(transform.position, diagonal_right * 100, Color.green);
if(Physics.Raycast(transform.position, diagonal_right, out hit, 100) )
{
Debug.Log ("Right Diagonal Hit!");
Debug.DrawRay(transform.position, diagonal_right * 100, Color.red);
}
}
}
Answer by TheDarkVoid · Jun 24, 2012 at 07:47 PM
try casting a ray forward, forward left, and forward right, and then compare the results in to make the camera turn when something is in front of it based on the raycast results.
Hi,
Going on what you're saying I have started a script that fires a ray in those 3 directions. However, while the forward-right diagonal (Vector3(1f, 0f, 1f).normalized) seems to detect colliders fine, the forward-left (Vector3(-1f, 0f, 1f).normalized) does not.
I've pasted the script above.
Any ideas?
Your answer
Follow this Question
Related Questions
Camera follow in c# 1 Answer
Camera to smoothly LookAt() a series of empty objects along a curved path that act as a 'rail' 3 Answers
Making a camera that follows a rigidbodied sphere. 2 Answers
Camera following/looking at aircraft 1 Answer
Camera Follow Object While Looking At Other Target 2 Answers