Hello people! Me and some friends are in the process of making a 3rd person adventure game, and I’m having problems with the camera collision. As the title reads, in our opinion, using Physics.Linecast is not good enough since the RaycastHit.point results in having some minor cutting in the surroundings. I’ve searched the forums for hours, and most people suggest using Linecast to solve the camera collision. Do you have any other ideas how to solve this problem (I’d rather not use rigidbody/collider). Is there a way to fake a radius of the RaycastHit.point so to get a better result?
Here’s the script, I would be really grateful for your assistance.
var layermask : LayerMask;
var target: Transform;
var height: float = 0.4;
var distance: float = 3.0;
var hit : RaycastHit;
var yMin : float = -4.0;
var yMax : float = 25.0;
var x : float = 0.0;
var y : float = 0.0;
private var xSpeed = 180.0;
private var ySpeed = 80.0;
private var minZoom: float = 1.0;
private var maxZoom: float = 4.0;
private var zoom: float = 2.0;
var v_pressed;
var scroll;
function Start() {
//The cameras starting position - behind the character.
var wantedRotationAngle = target.eulerAngles.y;
var currentRotationAngle = transform.eulerAngles.y;
currentRotationAngle = wantedRotationAngle;
x = currentRotationAngle;
}
function RotateBehindPlayer() {
//The camera rotates to the back of the character.
var wantedRotationAngle = target.eulerAngles.y;
var currentRotationAngle = transform.eulerAngles.y;
currentRotationAngle = wantedRotationAngle;
x = Mathf.Lerp(x, currentRotationAngle, Time.deltaTime * 7);
if (x == currentRotationAngle || Input.GetAxis("Mouse X"))
{
v_pressed = false;
}
}
function ScrollWheel() {
//Zoom (in/out).
if(Input.GetAxis("Mouse ScrollWheel") < 0)
{
if(zoom + 0.5 >= maxZoom)
{
zoom = maxZoom;
}
else
{
zoom = zoom + 0.5;
}}
if(Input.GetAxis("Mouse ScrollWheel") > 0)
{
if(zoom - 0.5 < minZoom)
{
zoom = minZoom;
}
else
{
zoom = zoom - 0.5;
}}
distance = Mathf.Lerp(distance, zoom, Time.deltaTime * 4);
}
function Update ()
{
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
if (y > yMax)
{
y = yMax;
}
else if (y < yMin)
{
y = yMin;
}
if (x >= 360)
{
x = 0;
}
if (x <= -360)
{
x = 0;
}
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, height, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
//When the key "V" is pressed, the function (RotateBehindPlayer) activates.
if (Input.GetKeyDown(KeyCode.V))
{
v_pressed = true;
}
if (v_pressed == true)
{
RotateBehindPlayer();
}
//The cameras adjustment to collision.
// if (Physics.Linecast(target.position, transform.position, hit, layermask))
// {
// var tempDistance = Vector3.Distance(target.position, hit.point);
// position = rotation * Vector3(0.0, height, -tempDistance) + target.position;
// transform.position = position;
// }
//When the scroll wheel is used, the function (ScrollWheel) activates.
if (Input.GetAxis("Mouse ScrollWheel"))
{
scroll = true;
}
if (scroll == true)
{
ScrollWheel();
}
}