Raycast hitting air
Hello everyone, im having a problem with raycast returning true hitting the air, i have a game object down my UFO and the camera is aiming at it. im raycasting from the game object to the camera position to detect any walls between them, if the recast find a wall it reposition the camera to not block the players vision of the "abduction point". the problem is that it is returning true just by getting close to the wall and not actually hitting it, and it bugs the player view witch gets kind of jumping in the corners, as my game involve a little labyrinth its impossible to avoid the corners, here is the camera code and some screenshots of the problem. thx for the attention ppl.
using UnityEngine; using System.Collections;
public class ShipCameraController : MonoBehaviour {
public GameObject follow;
private Vector3 camPosition;
public float distanceAway = 5.0f;
public float distanceUp = 1.0f;
public float smooth = 15.0f;
void Start(){
}
void LateUpdate(){
SmoothFollow();
}
void SmoothFollow(){
//Follow player smoothly
camPosition = follow.transform.position + follow.transform.up * distanceUp - follow.transform.forward * distanceAway;
CompensateForWalls();
transform.position = Vector3.Lerp (transform.position, camPosition, Time.deltaTime * smooth);
transform.rotation = Quaternion.Slerp (transform.rotation, follow.transform.rotation, 1);
transform.LookAt(follow.transform);
}
void CompensateForWalls(){
Vector3 correctionVector = follow.transform.position + follow.transform.up * distanceUp - follow.transform.forward * distanceAway;
RaycastHit hit;
//this is the line being drawn
Debug.DrawLine(follow.transform.position,correctionVector);
//this is the bugged raycast!!!
if(Physics.Raycast(follow.transform.position, correctionVector, out hit)){
print("debug");
camPosition = follow.transform.position + follow.transform.up * hit.point.y - follow.transform.forward * hit.distance;
}
}
}
Let's just say that Raycasts usually don't hit air :) Print the name of the object that gets hit with hit.transform.name. $$anonymous$$aybe that alone helps you already to find a solution. $$anonymous$$aybe you can move some objects that should never get hit to a separate layer and exclude that layer from the raycast.
hey thx for the answer, I've found the problem, i was using ray cast on the function and draw line for the debug, they are little different ;).