x


Problems with the script Smooth Follow

Well I added the script Smooth Follow, my camera to follow my character, because the problem is that there are some movements that I do with my character, the camera passes through leaving the ground to see what is inside. I would like to fix it so that when she turned the camera crash into the ground without the cross and did not cease to focus on character

Thx in Advance

more ▼

asked Feb 23 '11 at 12:31 PM

Unamine gravatar image

Unamine
362 14 17 27

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

One trivial addition to this could be to raycast to the next camera position. If the raycast hit anything, limit the movement to the hit position, and perhaps subtract some length yet to avoid the camera being "exactly on" the ground.

I don't have unity installed at the moment so I can't verify this code would work but basically you'd have two points. One represents the current location and the other represents the next location (that the camera wants to go to);

Vector3 prevPos; // Assume containing interesting data.
Vector3 nextPos; // Assume containing interesting data.
Vector3 deltaPos = nextPos - prevPos;

Vector3 direction = deltaPos.normalized;
float length = deltaPos.magnitude;

RaycastHit hit;
if (Physics.Raycast(prevPos, direction, out hit, length))
{
    nextPos = prevPos + direction * hit.distance;
    // Or just use nextPos = hit.point..
}

// Use nextPos that should now "clamp" to the terrain. 
// You probably want to subtract (direction * someAmount),
// to avoid the camera being placed exactly on the terrain.
// Make use of layermasks if you need to fine adjust collision rules.
more ▼

answered Feb 23 '11 at 12:36 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

I have also been struggling with this. If you could give a small example as to how you would implement this (not asking you to write it all out course, just giving me an idea of how I would write that). I'd of course be for ever thankful. :-)

Feb 23 '11 at 12:41 PM Joshua

Added some code that could illustrate this.

Feb 24 '11 at 02:44 AM Statement ♦♦

Thanks, I'll try your example

Feb 24 '11 at 10:58 AM Unamine
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2994
x664
x316
x207

asked: Feb 23 '11 at 12:31 PM

Seen: 1461 times

Last Updated: Feb 23 '11 at 12:31 PM