- Home /
Comment in existing question.
RE: RaycastHit and camera through walls
So the camera now reacts to the walls, but when it does, it starts "crawling" down the wall (when I reverse towards it) until it hits the ground and then it moves through the wall.
void LateUpdate() {
RaycastHit wallHit = new RaycastHit ();
Ray wallRay = new Ray (camera.transform.position, car.transform.position - camera.transform.position);
if (Physics.Raycast (wallRay, out wallHit, distance)) {
if (wallHit.collider.tag == "Wall") {
camera.transform.position = wallHit.point;
}
}
}
You are currently resetting the position of the camera every LateUpdate that you detect contact with the wall. I suspect you want to add a flag, and use it to only reset the position the FIRST update that you contact the wall. Obviously, reset this flag when contact with the wall is broken.
This reason this is happening is because your "car" transform is below the camera, so the hit-point on the wall is below the camera. It may be coincidentally stopping when it reaches the floor, but I'd bet,it's actually stopping when it reached the car's y position.
Another way to eli$$anonymous$$ate this effect would be to eli$$anonymous$$ate the Y axis when you create your ray. ($$anonymous$$odify a local copy of car's position to be at the same y coordinate as camera's position- so neither one is below the other. Use this modified vector when creating your ray.) Note: Using a horizontal ray like this would allow the camera to pass over walls that are shorter than the camera is high.
@YoungDeveloper, I think this should be a separate question. If he had not included a link to the other question, would you have closed it?
Yes, that's exactly what I was saying, but that's just my opinion. Ok, as you wish.
$$anonymous$$y pleasure. I'll just copy/paste first comment into original Q, so we can continue discussion if needed.
This logic doesn't seem to actually do anything different.
True, you are using a flag now to hold the boolean hit-result, but you are still updating the position every Fixed update that the camera is touching the wall.
Try na$$anonymous$$g your flag "hadContactLastUpdateFlag". That descriptive name should you help you figure out how to setup the logic. (you are CLOSE)