Real camera Smooth Follow

Hello there !

i have a problem with my smooth-following camera:

i play a crate that is in third person
a camera is following it with the script Smooth Follow
Crate can rotate using mouse

but here is my problem : how can i make that the camera cant go behind walls ? it doesn’t work with collider :roll:

Look this Topic about Wow Camera,
http://forum.unity3d.com/viewtopic.php?t=18073

the result that I liked most was:
WowCamera.cs
Posted for Paintbrush: Thu May 07, 2009 10:05 am[/b]
but I have a problem in it.
Can somebody help me in my error?
Please enter in link to see my result of this script.
http://www.universalgamesonline.com.br/unity3d/studies/wowcamera.cs.html
```
**//WowCamera.cs

//Credits to this script: “Paintbrush”

using UnityEngine;
using System.Collections;

public class WowCamera : MonoBehaviour
{

public Transform target;

public float targetHeight = 1.7f;
public float distance = 5.0f;

public float maxDistance = 20;
public float minDistance = .6f;

public float xSpeed = 250.0f;
public float ySpeed = 120.0f;

public int yMinLimit = -80;
public int yMaxLimit = 80;

public int zoomRate = 40;

public float rotationDampening = 3.0f;
public float zoomDampening = 5.0f;

private float x = 0.0f;
private float y = 0.0f;
private float currentDistance;
private float desiredDistance;
private float correctedDistance;

void Start ()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;

    currentDistance = distance;
    desiredDistance = distance;
    correctedDistance = distance;

    // Make the rigid body not change rotation
    if (rigidbody)
        rigidbody.freezeRotation = true;

}

/**
 * Camera logic on LateUpdate to only update after all character movement logic has been handled.
 */

void LateUpdate ()
{
// Don’t do anything if target is not defined
if (!target)
return;

    // If either mouse buttons are down, let the mouse govern camera position
    if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
    {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    }
    // otherwise, ease behind the target if any of the directional keys are pressed
    else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    {
        float targetRotationAngle = target.eulerAngles.y;
        float currentRotationAngle = transform.eulerAngles.y;
        x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    }

    y = ClampAngle(y, yMinLimit, yMaxLimit);

    // set camera rotation
    Quaternion rotation = Quaternion.Euler(y, x, 0);

    // calculate the desired distance
    desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
    desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
    correctedDistance = desiredDistance;

    // calculate desired camera position
    Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + new Vector3(0, -targetHeight, 0));

    // check for collision using the true target's desired registration point as set by user using height
    RaycastHit collisionHit;
    Vector3 trueTargetPosition = new Vector3(target.position.x, target.position.y + targetHeight, target.position.z);

    // if there was a collision, correct the camera position and calculate the corrected distance
    bool isCorrected = false;
    if (Physics.Linecast(trueTargetPosition, position, out collisionHit))
    {
        position = collisionHit.point;
        correctedDistance = Vector3.Distance(trueTargetPosition, position);
        isCorrected = true;
    }
   
    // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance
    currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp(currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance;

    // recalculate position based on the new currentDistance
    position = target.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -targetHeight, 0));

    transform.rotation = rotation;
    transform.position = position;

}

private static float ClampAngle(float angle, float min, float max)
{
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp(angle, min, max);
}

}**
```

the js and cs script doesn’t work very good :stuck_out_tongue:

I like the way the trees straighten when you get near them. its like they’re slouching and then stand at attention when they notice you approaching. :shock:

I dont understand.
you say that i dont can use Javascript and C# in the same Project ?

This is the Movimet Default of Tree in Unity. :stuck_out_tongue:

Of course, you didn’t know that ? :slight_smile:

o.O
i didnt know :shock:
humm, then the cause of my error is this.

i will try convert all my scripts to C# and test.

thanks friend.

please, if you have one camera Script same Wow, Perfect World, and if can share, i want ^^

lol, he was kidding (see the smiley face?) You can absolutely use Javascript and C# in the same project, no problem.

:?
i am confuse now.

anyway i am studying the new tutorial released in unitytutorials.com:
EVAC-CITY Tutorial
is amazing, i am learning very much ^^
http://www.unitytutorials.com/document/298/how-to-make-a-game-similar-to-evac-city-in-unity3d

but i somebody have any Camera same wow, perfect world, please share to we ^^

No !!! You can use c# and java script togetheer. Obviously…

not as well really, since one has to be compiled before the other if using get component. but thats easy to do if you put the first in the standard asset folder