I’m working on my third person camera code and have run into some issues regarding wall detection and adjustment.
I have a functional Raycast solution that tracks wall collisions and adjusts the camera relative to the player, except the camera code I have is built off code that has built in distance smoothing in its calculations, and it’s making my raycast “bounce” off colliders, messing up the raycast check.
void WallDetection()
{
float wallBuffer = 2f;
if (Physics.Raycast(TargetLookAt.position, -transform.TransformDirection(Vector3.forward), out hitInfo, Distance, walls))
{
Distance = hitInfo.distance - wallBuffer;
}
}
This is the distance calculation and smoothing code the camera is using:
void CalculateDesiredPosition()
{
// Evaluate distance
Distance = Mathf.SmoothDamp(Distance, desiredDistance, ref velDistance, DistanceSmooth);
// Calculate desired position
desiredPosition = CalculatePosition(mouseY, mouseX, Distance);
}
Vector3 CalculatePosition(float rotationX, float rotationY, float distance)
{
Vector3 direction = new Vector3(0, 0, -distance);
Quaternion rotation = Quaternion.Euler(rotationX, rotationY, 0f);
return TargetLookAt.position + rotation * direction;
}
void UpdatePosition()
{
var posX = Mathf.SmoothDamp(position.x, desiredPosition.x, ref velX, X_Smooth);
var posY = Mathf.SmoothDamp(position.y, desiredPosition.y, ref velY, Y_Smooth);
var posZ = Mathf.SmoothDamp(position.z, desiredPosition.z, ref velZ, X_Smooth);
position = new Vector3(posX, posY, posZ);
transform.position = position;
transform.LookAt(TargetLookAt);
}
I’m a novice coder so I’m probably missing something obvious.
I want to know how to either make it perform the same task without the smoothing issue, or preferably modify the Raycast code so that it takes the smoothing in mind when calculating collisions.
Any help is appreciated