Question by
JaimeBGarcia · Mar 08, 2016 at 01:25 PM ·
cameracollisioncolliderterrain3rd person camera
My 3rd Person camera doesnt collide with Terrain... Please Help
Hi mates, im on Unity 5 and i am making a 3rd person game. I have a Player and a camera that has a CameraOrbit script to follow and turn around the player. This is the CameraOrbit script:
using UnityEngine;
using System.Collections;
public class CameraOrbit : MonoBehaviour
{
public Transform target;
public float distance = 10.0f;
public float xSpeed = 5f;
public float ySpeed = 2.5f;
public float distSpeed = 10.0f;
public float yMinLimit = -20.0f;
public float yMaxLimit = 80.0f;
public float distMinLimit = 5.0f;
public float distMaxLimit = 50.0f;
public float orbitDamping = 4.0f;
public float distDamping = 4.0f;
private float x = 0.0f;
private float y = 0.0f;
private float dist;
void Start()
{
dist = distance;
Vector3 angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
void LateUpdate()
{
if (!target)
return;
Vector3 oPos = transform.position;
x += Input.GetAxis("Mouse X") * xSpeed;
y -= Input.GetAxis("Mouse Y") * ySpeed;
distance -= Input.GetAxis("Mouse ScrollWheel") * distSpeed;
y = ClampAngle(y, yMinLimit, yMaxLimit);
distance = Mathf.Clamp(distance, distMinLimit, distMaxLimit);
dist = Mathf.Lerp(dist, distance, distDamping * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(y, x, 0), Time.deltaTime * orbitDamping);
transform.position = transform.rotation * new Vector3(0.0f, 0.0f, -dist) + target.position;
}
float ClampAngle(float a, float min, float max)
{
while (max < min)
max += 360.0f;
while (a > max)
a -= 360.0f;
while (a < min)
a += 360.0f;
if (a > max)
{
if (a - (max + min) * 0.5f < 180.0f)
return max;
else
return min;
}
else
return a;
}
}
It works well, but the problem is that my camera pass through the Terrain, it doesnt collide with it... I have checked a lot of scripts but i dont find the solution. Can anyone help me? But please be so specific because i am noob on Unity. I dont have any collider or anything on the camera because i dont think it is the problem... Thank you so much mates :)
sin-titulo.png
(515.2 kB)
Comment