- Home /
Raycast Rotating Camera
I was trying to make a camera that would rotate to the right, left, front, or back of the player if the camera comes too close to an object but it's not working. Can anyone offer some insight?
var degrees = 180; var target : Transform; var dist = 0.3; function Update () { var hit : RaycastHit; var fwd = transform.TransformDirection(Vector3.forward); var back = transform.TransformDirection(-Vector3.forward); var right = transform.TransformDirection(Vector3.right); var left = transform.TransformDirection(-Vector3.right);
if (Physics.Raycast (transform.position, fwd, dist)) {
print ("There is something in front of the object!");
Debug.DrawLine (transform.position, hit.point);
// transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);
}
if (Physics.Raycast (transform.position, back, dist)) {
print ("There is something behind the object!");
Debug.DrawLine (transform.position, hit.point);
transform.RotateAround (target.position, Vector3.up, -degrees * Time.deltaTime);
}
if (Physics.Raycast (transform.position, right, dist)) {
print ("There is something on the right of the object!");
Debug.DrawLine (transform.position, hit.point);
transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);
}
if (Physics.Raycast (transform.position, left, dist)) {
print ("There is something on the left of the object!");
Debug.DrawLine (transform.position, hit.point);
transform.RotateAround (target.position, Vector3.up, -degrees * Time.deltaTime);
}
}
Transforms have a .forward, .right, and .up properties you can use ins$$anonymous$$d of TransformDirection. What do you mean "doesn't work"? Does nothing print? Does it rotate wrong?
It prints but it won't rotate I need it to rotate around the player to avoid colliding with objects.
Answer by Berenger · Dec 16, 2010 at 04:10 PM
Are you sure the radius of your character controller (I assume that's what you're using) is lesser than "dist" ? If radius = 0.5 and dist = 0.3, the ray will never reach outside of your character, thus never collide anything. And check where the transform.position is. It's likely to be at your character's feet, and you'll probably prefer the center -> renderer.bounds.center should work.
Hope that helps.
[ EDIT ]
It seems to work with that code. I removed the TransformDirection function for the direction variables and, less important, DrawLine second parameter is an end position, not a direction.
var degrees = 180; var target : Transform; var dist : float = 0.3f;
function Update () { var hit : RaycastHit; var fwd = transform.forward; var right = transform.right; var up = transform.up; var back = -fwd; var left = -right; var down = -up;
Debug.DrawLine (transform.position, transform.position + right, Color.red);
Debug.DrawLine (transform.position, transform.position + up, Color.green);
Debug.DrawLine (transform.position, transform.position + fwd, Color.blue);
if (Physics.Raycast (transform.position, fwd, dist)) {
print ("There is something in front of the object!");
Debug.DrawLine (transform.position, transform.position + hit.point);
// transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);
}
if (Physics.Raycast (transform.position, back, dist)) {
print ("There is something behind the object!");
Debug.DrawLine (transform.position, transform.position + hit.point);
transform.RotateAround (target.position, Vector3.up, -degrees * Time.deltaTime);
}
if (Physics.Raycast (transform.position, right, dist)) {
print ("There is something on the right of the object!");
Debug.DrawLine (transform.position, transform.position + hit.point);
transform.RotateAround (target.position, Vector3.up, degrees * Time.deltaTime);
}
if (Physics.Raycast (transform.position, left, dist)) {
print ("There is something on the left of the object!");
Debug.DrawLine (transform.position, transform.position + hit.point);
transform.RotateAround (target.position, Vector3.up, -degrees * Time.deltaTime);
}
}
You mean the camera gets stucked in wall and stuff ? The reason could be at frame f the wall is still far from the camera, but at frame f+1 the movement was too quick, the camera is inside the wall and raycast doesn't see faces with normal looking the other way. Try too increase the dist variable.
It gets jammed between transitions, moving back and forwards uncontrollably rather than settling at the end.
Hum, I don't see where that come from :( I tried on an empty project with the camera behind my player-capsule and child of it, but I didn't saw much problems. The camera movements might be a little rough though.
Answer by Kourosh · Dec 27, 2010 at 08:00 AM
What you're looking for is to avoid entering meshes, right? So why don't you simply apply a sphere collider with a desired radius to your camera so that whenever it hits an object it easily remains there or rolls over the object (since it's a sphere). I mean that can be a simple solution. Cause what if there are too many objects in your scene and raycasting can be very costly on the performance.
Tried that before. The main issue is the fact that the camera will simply go through the object roughly ins$$anonymous$$d of smoothly,when I'd rather it avoid the obstacles.
Your answer
Follow this Question
Related Questions
Raycast to transform.forward comes out rear of Gameobject 2 Answers
mouse cursor between two rays 3 Answers
i need to use the rotation value of the x axis on a camera 1 Answer
Setting object Rotation to RayCast line 1 Answer
Make an object translate/raycast a certain way disregarding it's rotation 1 Answer