- Home /
Raycast is not working
private void InputSensors()
{
Vector3 theta0 = (transform.forward);
Vector3 theta1 = (transform.forward + transform.right);
Vector3 theta2 = (transform.right);
Vector3 theta1dash = (transform.forward - transform.right);
Vector3 theta2dash = (-transform.right); //-(transform.right);
Ray r = new Ray(transform.position, theta0);
RaycastHit hit;
r.origin = transform.position;
if (Physics.Raycast(r, out hit))
{
theta0Sensor = hit.distance / 20;
print("0: " + theta0Sensor);
}
r.direction = theta1;
if (Physics.Raycast(r, out hit))
{
theta1Sensor = hit.distance / 20;
print("1: " + theta1Sensor);
}
r.direction = theta2;
if (Physics.Raycast(r, out hit))
{
theta2Sensor = hit.distance / 20;
print("2: " + theta2Sensor);
}
r.direction = theta1dash;
if (Physics.Raycast(r, out hit))
{
theta1dashSensor = hit.distance / 20;
print("1': " + theta1dashSensor);
}
r.direction = theta2dash;
if (Physics.Raycast(r, out hit))
{
theta2dashSensor = hit.distance / 20;
print("2': " + theta2dashSensor);
}
}
}
This code is giving compiler error, I am trying to put sensor, because I am building a self driving car as my school project, I am a beginner and I have no idea how to work with ray casts, I watched youtube tutorials but none of them worked. Help please.
Answer by Bhavishya007 · Oct 24, 2020 at 11:39 AM
Vector3 fwd = transform.TransformDirection(Vector3.forward);
Vector3 rgt = transform.TransformDirection(Vector3.right);
Vector3 lft = transform.TransformDirection(-(Vector3.right));
Vector3 rgtfwd = transform.TransformDirection(Vector3.forward + Vector3.right);
Vector3 lftfwd = transform.TransformDirection(Vector3.forward - Vector3.right);
Vector3 rrr = transform.TransformDirection(-(Vector3.forward));
Vector3 rgtrrr = transform.TransformDirection(-(Vector3.forward) + (Vector3.right));
Vector3 lftrrr = transform.TransformDirection(-(Vector3.forward) - (Vector3.right));
RaycastHit hit;
Ray r = new Ray(transform.position, fwd);
r.origin = transform.position;
if (Physics.Raycast(transform.position, fwd, out hit, 10))
{
print("fwd: " + hit.distance);
}
if (Physics.Raycast(transform.position, rgt, out hit, 10))
{
print("rgt: " + hit.distance);
}
if (Physics.Raycast(transform.position, lft, out hit, 10))
{
print("lft: " + hit.distance);
}
if (Physics.Raycast(transform.position, rgtfwd, out hit, 10))
{
print("rgtfwd: " + hit.distance);
}
if (Physics.Raycast(transform.position, lftfwd, out hit, 10))
{
print("lftfwd: " + hit.distance);
}
if (Physics.Raycast(transform.position, rrr, out hit, 10))
{
print("rrr: " + hit.distance);
}
if (Physics.Raycast(transform.position, rgtrrr, out hit, 10))
{
print("rgtrrr: " + hit.distance);
}
if (Physics.Raycast(transform.position, lftrrr, out hit, 10))
{
print("lftrrr: " + hit.distance);
}
I don't know how, but this is working just fine now.
Answer by Bhavishya007 · Oct 19, 2020 at 06:16 PM
Just fix all compiler errors, nothing in particular.. although now something's wrong with it and it's not even taking scripts that were working earlier.
It's rare, but it's possible that after editing your scripts and fixing issues, Unity could not be importing them correctly. Right click your scripts folder and re-import it. If that doesn't work, there's likely still compiler errors with your scripts- check the output window carefully.
When you are able to add the script to your GameObject again, please edit the original question with the new code.
So, I did reimported all of them and it didn't work and then later I deleted the scripts and copied the codes from back up but it still didn't work, Then aI created the project from scratch but I pasted the same codes, this time it worked, but still, raycast aren't working, they just aren't at all.
Your answer
Follow this Question
Related Questions
Problem With Raycasthit Angle 0 Answers
Something fishy with RaycastHit 1 Answer
How can I can I cast a ray from a gameobject? 1 Answer
Fall collision force with help of raycast 0 Answers
RayCast2d Not colliding with objects in other layers 1 Answer