Question by
VeryDisappointingPineapple · Oct 28, 2018 at 09:00 AM ·
c#terrainbuttoncamera-movementcamera rotate
How to prevent camera going into terrain
My camera is supposed to follow the ball, which it does perfectly and get further away/closer when scrolling. The movement is fine and I am happy with it, however the camera can freely move under the terrain. I have tried to prevent this with blocking movement under 100 (where my terrain is) but this doesn't work because I want to have displacements or a not completely flat area and for the camera to stop on any part of the terrain. I am using a first person character and the script of my current movement is below!
using UnityEngine;
public class BallFollow : MonoBehaviour {
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
public float fieldOfView = 5.00f;
void LateUpdate ()
{
offset = -(transform.forward * fieldOfView);
transform.position = target.position + offset;
if (Input.GetAxis("Mouse ScrollWheel") > 0 && fieldOfView > 0.5f)
{
fieldOfView = fieldOfView - 0.5f;
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 && fieldOfView < 10.0f)
{
fieldOfView = fieldOfView + 0.5f;
}
}
}
unity.png
(284.0 kB)
unity2.png
(231.4 kB)
Comment