- Home /
Unity raycast not working / returning weird value
I am trying to make a third person camera, so far everything is working. But the camera can go through objects. So I tried to make a script that casts a ray and adjusts the camera position. I tried for hours to get it to work but it just doesn't. Here is my script: RaycastHit hit;
float dis = 10f;
if (Physics.Raycast(transform.position,new Vector3(Pitch*-1,Direction-180,0), out hit,2f))
{
dis = hit.distance;
Debug.Log("hit! distance: " + hit.distance + " position: " + hit.point);
}
Vector3 pos = Quaternion.Euler(Pitch, Direction, 0) * new Vector3(dis/2f*-1f,dis/2f*1f,dis/2f*-2f);
GameObject.Find("Camera").transform.position = gameObject.transform.position + pos;
The only object that has a collider is the plane directly under to player. Oh and I found something strange: The ray only returns true at some angels, even if the angle is the same but with 360 degrees more, it still returns false.
new Vector3($$anonymous$$ch*-1,Direction-180,0)
will always be in world space, no matter where the camera looks. Also, what should the direction be? Looks like some random direction. What are $$anonymous$$ch and direction?
Your answer
Follow this Question
Related Questions
RE: RaycastHit and camera through walls 0 Answers
camera detecting walls 1 Answer
Raycast hitting objects to the left of my player 1 Answer
Camera/mouse movement issues "almost but no dice" 1 Answer
Some GameObjects can't touch !!! 0 Answers