Hey, first of all, i am a programming noob and i have written my own third person camera script. My problem is: i have no idea how i prevent glitching through walls.
Sorry for my poor english!
This is my ThirdPersonCameraScript.cs:
using UnityEngine;
using System.Collections;
public class thirdPersonCamera : MonoBehaviour {
public Transform cameraTarget;
public Texture2D crossHair;
public float distance = 2.0f;
public float xSpeed = 200.0f;
public float ySpeed = 100.0f;
public float yMinLimit = -40.0f;
public float yMaxLimit = 40.0f;
public float zoomMinLimit = 1.0f;
public float zoomMaxLimit = 2.5f;
private float rotationX = 0.0f;
private float rotationY = 0.0f;
void Start () {
Vector3 angles = transform.eulerAngles;
rotationY = angles.x;
rotationX = angles.y;
}
void OnGUI () {
GUI.DrawTexture(new Rect(Screen.width / 2 + 25, Screen.height / 2 + 25, 50, 50), crossHair, ScaleMode.ScaleToFit, true, 1.0F);
}
void Update () {
if (Input.GetAxis("Mouse ScrollWheel") > 0 distance > zoomMinLimit)
{
distance = distance - 1 / (xSpeed * 0.02f);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0 distance < zoomMaxLimit)
{
distance = distance + 1 / (xSpeed * 0.02f);
}
rotationX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
rotationY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
rotationY = Mathf.Clamp(rotationY, yMinLimit, yMaxLimit);
Vector3 lol = new Vector3(0.0f, 0.0f, -distance);
Quaternion cameraRotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotationY, rotationX, 0), Time.smoothDeltaTime * 3);
Vector3 cameraPosition = cameraRotation * lol + cameraTarget.position;
transform.rotation = cameraRotation;
transform.position = cameraPosition;
}
}
Can somebody help me please?