- Home /
Camera Collision Lerp Problem
Hello there! I'm working on a 3D Jump'n'run. I have a camera script that uses Linecasts to move the camera closer to my Player if the Line collides with the layer that i set up for this purpose. Although the camera lerps towards it's target, it seems to stop lerping for no reason and can still be moved through the ground.Even though the Linecast still collides with my layer.
How do I prevent the camera from doing this?
Any help is appreciated, I thank you in advance :)
Here is a video that further illustrates my problem
(note: to prevent this behaviour, i have tried a few things myself, such as:
adding a rigidbody to the camera and using .addforce on collision
on collision: freezePosition and freezeRotation with the Rigidbody mentioned above
inverting x and y input on collision
add a float value to x and y on collision
multiply the values of x and y on collision
applying transform.translate on collision(with, corresponding to the line that collided, a vector3 "pulling" in the opposite direction)
and, at least the way i tried it, all these attempts failed.)
Here is my Script:
using UnityEngine;
using System.Collections;
public class MouseOrbitCSHARP : MonoBehaviour
{
public Transform Target;
public float Distance = 20.0f;
public float DistanceMin = 7.5f;
public float DistanceMax = 25f;
public float xSpeed = 200.0f;
public float ySpeed = 200.0f;
public float yMinLimit = -80.0f;
public float yMaxLimit = 80.0f;
private float x;
private float y;
public LayerMask layermask;
void Awake()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
Screen.lockCursor = true;
}
void LateUpdate()
{
if(Target != null)
{
x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -Distance)) + Target.position;
transform.rotation = rotation;
transform.position = position;
Vector3 topLeft = (new Vector3(transform.position.x-2,transform.position.y+2,transform.position.z+5));
Vector3 topRight = (new Vector3(transform.position.x+2,transform.position.y+2,transform.position.z+5));
Vector3 botRight = (new Vector3(transform.position.x+2,transform.position.y-2,transform.position.z+5));
Vector3 botLeft = (new Vector3(transform.position.x-2,transform.position.y-2,transform.position.z+5));
//Debugging the main line to the camera transform and the 4 lines related to the positions around the camera transform
Debug.DrawLine(Target.position,transform.position, Color.green);
Debug.DrawLine(Target.position,topLeft, Color.green);
Debug.DrawLine(Target.position,topRight, Color.green);
Debug.DrawLine(Target.position,botLeft, Color.green);
Debug.DrawLine(Target.position,botRight, Color.green);
//Cast lines, lerp on collision
if (Physics.Linecast(Target.position,transform.position,layermask))
{
Debug.DrawLine(Target.position,transform.position, Color.red);
Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
if(Distance == DistanceMin)
{
}
}
else if (Distance < DistanceMax && !Physics.Linecast(Target.position,transform.position,layermask) )
{
Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//top Left
if (Physics.Linecast(Target.position,topLeft, layermask))
{
Debug.DrawLine(Target.position,topLeft, Color.red);
Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
Debug.Log ("topLEFT");
if(Distance == DistanceMin)
{
}
}
else if (Distance < DistanceMax && !Physics.Linecast(Target.position,topLeft,layermask) ) // there it is!
{
Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//top right
if (Physics.Linecast(Target.position,topRight,layermask))
{
Debug.DrawLine(Target.position,topRight, Color.red);
Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
Debug.Log ("topRIGHT");
if(Distance == DistanceMin)
{
}
}
else if (Distance < DistanceMax && !Physics.Linecast(Target.position,topRight,layermask)) // there it is!
{
Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bottom left
if (Physics.Linecast(Target.position,botLeft,layermask))
{
Debug.DrawLine(Target.position,botLeft, Color.red);
Debug.Log ("bottomLEFT");
Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
if(Distance == DistanceMin)
{
}
}
else if (Distance < DistanceMax && !Physics.Linecast(Target.position,botLeft,layermask) )
{
Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//bottom right
if (Physics.Linecast(Target.position,botRight,layermask))
{
Debug.DrawLine(Target.position,botRight, Color.red);
Debug.Log ("bottomRIGHT");
Distance = Mathf.Lerp(Distance, DistanceMin, Time.deltaTime * 1.0f);
if(Distance == DistanceMin)
{
}
}
else if (Distance < DistanceMax && !Physics.Linecast(Target.position,botRight,layermask)) // there it is!
{
Distance = Mathf.Lerp(Distance, DistanceMax + 0.1f, Time.deltaTime * 1.5f);
}
}
}
private float ClampAngle(float angle, float min, float max)
{
if(angle < -360)
{
angle += 360;
}
if(angle > 360)
{
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
}
Your answer

Follow this Question
Related Questions
Camera Stutter During Distance Correction 0 Answers
Return 3rd Person camera to original position 0 Answers
Weird artifacts in cg shader with lerp() 1 Answer
Lerp isn't completing itself 3 Answers