Camera raycast?

hey all! i was doing peteys from burgzergarcades tutorial series camera part and when he made his camera he didnt add collision. i was wondering if you guys wanted to help me figure this out i want the camera to collide with everything in the game and i heard some people using raycast. i am noobie at scripting and never done raycasting but this is it and sorry if it gets cut off half way through dont know hot to fix that :

using UnityEngine; using System.Collections;

public class RPGCamera : MonoBehaviour { public Transform target; public float walkDistance; public float runDistance; public float height; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float heightDamping = 2.0f; public float rotationDamping = 3.0f;

private Transform _myTransform;
private float x;
private float y;
private bool _camButtonDown = false;

void Awake() {
    _myTransform = transform;   

// Use this for initialization
void Start () {
    if(target == null) {
        Debug.LogWarning("We do not have a target for the camera");
    else {

void Update() {
        _camButtonDown = true;
        _camButtonDown = false;

void LateUpdate() {
    if(target != null) {

        if(_camButtonDown) {        

            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

//          y = ClampAngle(y, yMinLimit, yMaxLimit);

            Quaternion rotation = Quaternion.Euler(y, x, 0);
            Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;

            _myTransform.rotation = rotation;
            _myTransform.position = position;

        else {

// _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); // _myTransform.LookAt(target); x = 0; y = 0;

            // Calculate the current rotation angles
            float wantedRotationAngle = target.eulerAngles.y;
            float wantedHeight = target.position.y + height;

            float currentRotationAngle = _myTransform.eulerAngles.y;
            float currentHeight = _myTransform.position.y;

            // Damp the rotation around the y-axis
            currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

            // Damp the height
            currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

            // Convert the angle into a rotation
            Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

            // Set the position of the camera on the x-z plane to:
            // distance meters behind the target
            _myTransform.position = target.position;
            _myTransform.position -= currentRotation * Vector3.forward * walkDistance;

            // Set the height of the camera
            _myTransform.position = new Vector3 (_myTransform.position.x, currentHeight, _myTransform.position.z);

            // Always look at the target
            _myTransform.LookAt (target);


public void CameraSetup() {
    _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance); 



If you want the camera to collide, simply add a collider and a rigidbody. That way it will collide with the other objects in the scene. Unless you wanted some other specific behavior that isn't clear from your question...

Easy. Make a private float called distanceToGround. Put this code in the Update function:

if (Physics.Raycast(_myTransform.position, -Vector3.up, out hit, 100.0f)) {

distanceToGround = hit.distance;

Then, times the walk distance in the Vector3 position variable by distanceToGround. Then you shouldn’t have any errors. You might want to edit my code as I spent all day working on it and it isn’t perfect, but it’s good enough for now.

(if post doesn’t come out right, please edit).