Hi folks,
I’ve been looking at some 3rd person camera scripts provided in tutorials but can’t seem to figure them out and would like to know how to get a simple camera follow script.
Am I wrong in thinking it has something to do with the distance between my camera and the target object?
any help is appriaciated, thanks.
What is it that you’re looking to have different than the camera in the 3D platformer tutorial? Is it the distance you want to change, the rotation/look at behavior? Let us know and we’ll be glad to help.
Red_5,
What are you doing currently?
Have you dragged the camera script on to your camera? Yes? Then you need to look in the inspector on the right side and make changes to the ‘exposed’ variables there relating to the Target your camera is following - this will be a game object. Drag the actual object name from your hierarchy on to the Target setting which will be null just now. Your camera now knows which object to follow. You can set that TARGET in code too, if you wish.
You probably need to alter the DISTANCE and HEIGHT parameters to make the camera follw your object at the correct distance and height away from your object.
Hey guys, thanks for getting back to me.
I’ve decided not to use the scripts provided until I can work them out myself so my current code that i’ve added myself is:
var target : Transform;
// Rotate the camera every frame so it keeps looking at the target
function Update() {
transform.LookAt(target);
}
I know this is a look at script but I was playing around with the
transform.position = Target.position;
which places the camera in the same position as the gameObject/target so I’m wondering how I can then set its position to be behind it. However, I can see its assuming the position of the object its targeting so i’m proobably going about it the wrong way.
heres what i’d like my camera to do;
- Follow the player(game object)
- not to rotate when my game object rotates
anyway thanks in advance
Here’s the first ever script I wrote for Unity, it’s a VERY SIMPLE 3rd person camera controller.
It’s different than most TPS cam controllers in that it is designed for a point and click game, and therefore does not automatically move with the character himself, unless he gets out of range.
But it is VERY BASIC to make it easy to understand what’s going on. It looks a bit more complex though due to all the comments. paste it into a color-coding editor and you’ll see how simple it actually is.
I wrote comments for every line of code.
The trickiest thing about it is the threshold, and the acceleration.
// Put this script on a GameObject with your camera and a character controller on it.
var target: GameObject; // Assign this to your player
var maxDist = 20.0; // distance before triggering follow
var minDist = 10.0; // distance before untriggering follow
var speed = 6.0; // Should be set to the same speed as your character
private var acc = 0.0; // Amount of movement on the camera. this starts at 0 and builds until it reaches speed.
function LateUpdate () // Had to be late update becuase it was studdering otherwise.
{
var myDist = 0; // This variable is a (changing) threshhold to determine whether or not our camera should be moving or not.
// when the camera is not moving (acc==0), it is asigned to maxDist.
// Once the camera starts moving, we set myDist to minDist. This means the camera needs to get even CLOSER to the player before it stops
// moving than it took to trigger it in the first place. this behavior does 2 things:
// When the camera is stopped, it lets the character walk around a bit before the cam starts to move.
// When the camera is moving, it prevents studdering as the character walks in-and-out of threshold.
if (acc == 0) // If the camera is not moving
{
myDist = maxDist; // Set our threshhold to Max
}
else // else the camera IS moving
{
myDist = minDist; // set threshold to min
}
transform.LookAt(target.transform); // Aim at the character
var idist = Vector3.Distance(transform.position, target.transform.position); // Get distance between the cam and the player
if (idist > myDist) // If distance beyond our current threshold
{
acc = Mathf.Min(acc + 2, speed); // accelerate by a fixed amount (+2) until we're at max speed (speed)
}
else // Else the distance is inside our threshold
{
acc = Mathf.Max(acc - 2, 0); // deacclerate by (-2) until we're at stopped (0).
}
if (acc > 0) // If we're moving
{
//transform.Translate(0, 0, acc * Time.deltaTime); // Old way without collision;
//--VV-- This turns the local angle into a world coord that can be passed to the Move command
var iangle = transform.TransformDirection(Vector3(0,0, acc * Time.deltaTime));
//--AA--
var controller : CharacterController = GetComponent(CharacterController); // Get controller
var flags = controller.Move(iangle);// Move with collision but without gravity.
}
}
Thanks for that hanford, i’ll take a look.
BTW,
The easiest way to do a 3rd person camera is to make the camera and character a child of a GameObject. You don’t even need code at that point … the camera will just stay lock-step to the character no matter what.
Yeah I tried that but I didn’t want the camera to rotate when my GameObject rotates
I’ll just add some more to this instead of making a new thread
I’m trying to see if the idist is the same as my fdist
var fdist = 3.070831;
var idist = Vector3.Distance(transform.position, target.transform.position);// Get distance between the cam and the player
if (fdist == idist ) {
print ("distance is the same:" );
}
so I’ve set the fdist to the idist but when I try and see if there the same nothing is coming up.
hopefully if they are the same I can then figure out how to get the camera to stay in that position then i’ll have that eureka moment i’ve been longing for.
Thanks for the help so far guys, very much appreciate it.
I’m new to Unity, but I’ve had problems testing equality with floats in other languages.
I found this:
Mathf.Approximately()
In fact in the unity documents it states: