Hello GameDev Community,
i've got a small problem with figuring out a way to do cameracliping in the unityengine with raycasting i got two main problems the first being that my camera flicks nervously between two spots when it comes to wall contact the second being that the camera gets stuck on one spot on a wall and only gets out of it at random.
using UnityEngine;
using System.Collections;
public class cameracontroller : MonoBehaviour
{
GameObject player;
public GameObject camera;
public Vector3 cameramaximumrange;
public Vector3 cameraposition;
public GameObject cameramaximum;
public GameObject cameraminimum;
public Vector3 cameraDirection;
float distance;
public RaycastHit info;
public float factor = 0.5f;
Ray ray;
void Start()
{
player = GameObject.Find("player");
camera = transform.FindChild("runcam").gameObject;
cameramaximum = player.transform.FindChild("cameramaximum").gameObject;
cameraminimum = player.transform.FindChild("cameraminimum").gameObject;
ray = new Ray();
}
void LateUpdate()
{
transform.LookAt(player.transform.position);
transformCamera();
distance = Vector3.Distance(player.transform.position,camera.transform.position);
cameraDirection = camera.transform.position - player.transform.position;
ray.direction = cameraDirection;
ray.origin = player.transform.position;
camera.transform.LookAt(player.transform.position);
}
public void transformCamera()
{
if (Input.GetMouseButton(1))
{
transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X") * 50 * Time.deltaTime, 0));
factor += Input.GetAxis("cameravertical");
cameramaximumrange = Vector3.Lerp(cameramaximum.transform.position, cameraminimum.transform.position, factor);
if(Physics.Raycast(ray,out info,distance,99))
{
Debug.Log(info.point);
cameraposition = info.point;
}else
{
cameraposition = cameramaximumrange;
}
camera.transform.position = cameraposition;
}
}
}
this is the code I use to manage my camera movement
if anybody knows a solution to these problems or a better way to do camera clipping I would be very thankful
with kind regards,
mineorbit