- Home /
Raycast goes through collider
Hello,
I'm using raycasts to shoot a rocket. However, it goes straight through colliders sometimes. I checked some of the basics and made sure my code was in FixedUpdate and made sure my distance wasn't too small. Also, tried debugging rays and they go straight through the collider and still no collision. Here is my code:
void FixedUpdate () {
movement = (forward.normalized * force) - downMovement + boost;
nextPosition = raycastSpot.position + movement;
distance = Vector3.Distance (raycastSpot.position, nextPosition);
distance += 10f;
rotation = Quaternion.LookRotation (movement.normalized, Vector3.up);
Debug.DrawRay (raycastSpot.position, movement, Color.white);
RaycastHit hit;
if(Physics.Raycast(raycastSpot.position, rotation.eulerAngles, out hit, distance)){
//This is not called VV
hitSomething (hit.point, hit.normal);
}else{
transform.position = nextPosition - (movement.normalized * offset);
transform.rotation = rotation;
}
Debug.DrawRay (raycastSpot.position, movement, Color.blue);
if (boost.y > 0f) {
boost -= boostSubtract;
} else {
boost = Vector3.zero;
}
}
"boost" is just a vector3 adding some upward force at first that is taken away over a few frames by "boostSubtract"
The blue ray is just to give me and idea where the rocket will be next frame, even though it is slightly off.
The white ray tells me exactly where is was at the start and end of the frame.
Pictures:
It does work sometimes, I have two more pictures showing it colliding into the terrain, but I cant upload any more pictures on this post.
Is the collider marked as a trigger in the inspector? If it is then it probably should be set to not being marked as a trigger.
Also, is the collision box actually the same size as the cube your trying to hit in those pictures? Its possible they are colliding just doing so inside the mesh, resulting in it being unclear if they have or not.
Other than those two possible causes I don't really know whats going on here.
Whoops, I didn't see your comment until just now. The collider is the same size as the cube and is not a trigger. Thanks for replying.
Did you check your Physics Settings of your project. Check Layer Collision $$anonymous$$atrix and also make sure that cube has not layer of "Ignoredraycast"
I checked and the cube and rocket are both on "default" and the collision matrix had collision between the layers, I'm thinking it's something with my code that I'm not seeing.
Answer by Bunny83 · Jul 21, 2018 at 09:19 AM
Physics.Raycast needs a position vector for the starting point and a direction vector as direction, You passed in the eulerAngles of that object. This is not a direction at all. It's just a collection of 3 angles. If you want to cast a ray into the forward direction of your object, just use transform.forward
if(Physics.Raycast(raycastSpot.position,transform.forward , out hit, distance)){
However since you actually use "movement" vector to move your object you may just use this vector as it's the direction you would move along
if(Physics.Raycast(raycastSpot.position, movement, out hit, distance)){
Answer by AdityaViaxor · Jul 21, 2018 at 11:43 AM
make sure that your collision detection in rigidbody in continues it is often mistake that we just add rigidbody and the collision detection is by default set it discrete to sometime it collide and sometime it doesn't also check whether both are not set to isTrigger ture this also a reason for passing through object.
Came here with the same problem and this was my issue. Thanks for having commented this in 2018.
Your answer
Follow this Question
Related Questions
Physics raycast hit offset from where it should be 1 Answer
Raycast exit point of collider 0 Answers
RaycastHit collider not showing correct tags 1 Answer
Raycast passes Collider 0 Answers