- Home /
How to stop 3rd person camera going throught walls ?
Hello I am strugling with a 3rd person camera controll.
I want the camera to get closer to the player when it hits the wall or any obstacle.
But i don't know how I've tried putting colliders onto it but it doesn't work.
Does anybody knows how to fix this ?
This is my code ->
public float mouseSensitivity = 10; public Transform target; public float distanceFromTarget = 2; public Vector2 MinMaxViewAngle = new Vector2( -40,85);
public float rotationSmoothTime = .12f;
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
float Xrotation;
float Yrotation;
// Update is called once per frame
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void LateUpdate ()
{
Xrotation += Input.GetAxis("Mouse X") * mouseSensitivity;
Yrotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
Yrotation = Mathf.Clamp(Yrotation, MinMaxViewAngle.x, MinMaxViewAngle.y);
currentRotation = Vector3.SmoothDamp(currentRotation, new Vector3(Yrotation, Xrotation), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = target.position - transform.forward * distanceFromTarget;
}
Answer by JonPQ · Aug 28, 2017 at 07:25 PM
1) remove the colliders. 2) position/ move your lead game object.... e.g. car or character. 3) generate a vector backwards from your character, (maybe using combination of character's facing, and up vectors) in the direction you want the camera to be, and also generate the point along that vector at the distance you want the camera to be from the object... say 10 unity units. 4) Then use Physics.raycast( out hit) Which outputs collision data... if there is no collision... set your camera at that point... and set its transfor.forward to point at your character (the other direction along the vector) 5) If there is a collision (or N collisions) it will give you a list of collision points in the 'out' Choose the closest one, to position your camera there.
one extra tip, use Physics.spherecast ins$$anonymous$$d of rayscast... OR subtract a bit of extra distance from the collision point back along the collision vector... This will keep the camera away from the wall a bit.
Iˇve managed to do it other way. I´ve created a gameobject that holds the camera and did a raycast in all of its directions and when it hits something move the way out of the wall (push it from the wall) then i was able to update cameras location and when it stopped hitting the wall , move it back to its original position and sent a raycast towards the player that warns that it returned to its original position.
Thanks for the advice,You helped alot
Glad to be of assistance :) a couple more tips... the raycast has a layer paramater... Raycasting is slow... so you can make simple collision layer with boxes... ins$$anonymous$$d of complex geometry.... put them in a (non-rendered) layer (cameras use the layers also) then do the raycast only to that layer.... That will get you a big speed up on the raycasting.
The other simple tip, is try not to use too many ray casts.... it can be slow ! at least try to limit it to one ray or sphere cast... until it looks like there is a collision you cant resolve easily... then add in the extra ray casts. :)