- Home /
Third Person Camera Movement control.
Hi, In third person controller, the camera movement and rotation based on character movement and rotation.When the character goes near to the wall and rotate in any of the direction,camera goes inside the wall.How to control my camera entering the wall or mesh.
Answer by fddefdef · Jan 09, 2015 at 02:07 PM
You could use a raycast starting from the camera that tells it when there's a wall nearby.
share the reference link for using raycast to detect the wall.
http://docs.unity3d.com/ScriptReference/Physics.Raycast.html here you find all the information you need to use a raycast.
still camera go through the wall,camera's position and the rotation moves based on thirdperson movement.When thirdperson move near the wall and rotate in any of the direction,camera going through the wall.
Tried by adding Collider and rigidbody to the camera,but still camera going through the wall.Any solution for it..
Answer by sas_88 · Jan 23, 2015 at 11:45 AM
enter code here
Finally find the solution for avoiding Thirdperson camera entering the wall or mesh.Use Physics.Raycast to detect the object or wall opposite to Thirdperson.
Vector3 orgpos=new Vector3(0.0f,0.0f,0.0f);
Vector3 objpos = transform.TransformPoint(0, height, -distance);
Transform cameraTransform;
void Update()
{
RaycastHit hit;
Vector3 back = transform.TransformDirection (-1 * Vector3.forward);
if (Physics.Raycast (transform.TransformPoint (orgpos), back, out hit, distance) && hit.transform != transform) {
objpos.x = hit.point.x;
objpos.z = hit.point.z;
objpos.y = Mathf.Lerp (hit.point.y, objpos.y, Time.deltaTime);
}
cameraTransform.position = Vector3.Lerp (cameraTransform.position, objpos, Time.deltaTime);
}
Add the script to player and assign camera as target.