- Home /
Camera goes through walls when moving fast.
Hello, I have a dynamic camera that follows it's target but passes through walls when I move it very fast. My script works but It isn't working I supposed to. Where is my problem exactly? Any help is really appreciated.
Here is my script.
public GameObject target;
public float distance;
public float xSpeed = 2.0f;
public float ySpeed = 2.0f;
public float yMinLimit = -90f;
public float yMaxLimit = 90f;
public float distanceMin = 15f;
public float distanceMax = 10f;
public float smoothTime = 5f;
public float camCollisionOffset = 1;
float rotationYAxis = 0.0f;
float rotationXAxis = 0.0f;
float velocityX = 0.0f;
float velocityY = 0.0f;
public LayerMask CamOcclusion;
private Vector3 offset;
// Use this for initialization
void Start()
{
Vector3 angles = transform.eulerAngles;
rotationYAxis = angles.y;
rotationXAxis = angles.x;
offset = transform.position - target.transform.position;
if (GetComponent<Rigidbody>())
{
GetComponent<Rigidbody>().freezeRotation = true;
}
}
void FixedUpdate()
{
}
void LateUpdate()
{
if (target)
{
if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
{
velocityX += xSpeed * Input.GetAxis("Mouse X") * distance * 0.02f;
velocityY += ySpeed * Input.GetAxis("Mouse Y") * 0.02f;
}
rotationYAxis += velocityX;
rotationXAxis -= velocityY;
rotationXAxis = ClampAngle(rotationXAxis, yMinLimit, yMaxLimit);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, rotationYAxis, 0);
Quaternion rotation = toRotation;
float nowDistance = distanceMin;
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.transform.position;
transform.rotation = rotation;
transform.position = position;
Ray ray = new Ray(transform.position, -transform.forward);
RaycastHit hit;
if (Physics.Raycast(ray,out hit,distanceMin+camCollisionOffset, CamOcclusion))
{
nowDistance = hit.distance - camCollisionOffset;
}
distance = Mathf.Lerp(distance,nowDistance,50* Time.deltaTime);
velocityX = Mathf.Lerp(velocityX, 0, Time.deltaTime * smoothTime);
velocityY = Mathf.Lerp(velocityY, 0, Time.deltaTime * smoothTime);
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
Answer by Dray · Nov 24, 2017 at 10:20 AM
Use Rigidbody.MovePosition for moving your camera. If you do so, physics will be taken into account.
What you are doing right now, since you are setting the transforms position by hand, is "teleporting" the camera over a tiny distance each frame, ignoring all colliders and forces around it.
Also make sure the camera has a collider and move the part where you are changing the actual position into FixedUpdate for higher accuracy.
EDIT to integrate the comments, try adding this to your component
public Vector3 cameraMovement;
public float cameraRadius = .5f;
void FixedUpdate()
{
var frameMovement = cameraMovement * Time.fixedDeltaTime;
RaycastHit hit;
if (Physics.SphereCast(camera.transform.position, cameraRadius, frameMovement, out hit, frameMovement.magnitude))
{
camera.transform.position = hit.point + hit.normal * cameraRadius;
} else {
camera.transform.position += frameMovement;
}
}
Let me know if this works
As much as possible I don't want to use Rigidbody on my camera. Is there any way to do this?
Save the camera's current position. In the next frame, cast a ray from the now-current position to the saved last-current position. If it hits anything, move the camera to the hit.point of the ray.
Right that will work too, you can even use Physics.SphereCast to give your camera some kind of radius
Answer by awts202 · Dec 03, 2017 at 07:27 PM
I found the problem, my camera only moves when the camera hits something but won't move when it doesn't hit anything. I should make my camera move closer to my target when something block camera's view. How to accomplish this? Pls help. Thnks in advance.
Your answer
Follow this Question
Related Questions
Camera collision using raycasts (problems with SmoothDamp) 1 Answer
How to validate camera when collides with a wall - ThirdPerson 2 Answers
How to make camera position relative to a specific target. 1 Answer
Accessing all GameObjects with a certain tag 1 Answer
nullifying targetObject after a Smooth LookAt Transition 2 Answers