Simple 3rd Person Camera Controller in Unity




Someone in the Unity Forums asked for help with a 3rd person camera controller. I decided to post my code there for him/her, and so I'm going to post it here, too. This is a javascript that will track an object in the world and move the camera towards it if the object gets too far away.

I like code examples that are simple and easy to read, and while this code could be even more stripped down, it's pretty simple. I included comments for every line.

// 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. 
		}
	}


And that's all!



Feedback - 6 responses

Displayed newest to oldest. Leave a comment.
delaware insurance department wrote:   
It is good post that i seemed to be awaiting pertaining to such an post and i also possess acquired several handy info using this website. Many thanks for discussing this review.I simply want to keep the remark as a token regarding thanks. I must take this chance to post back from my personal soul.I must say i loved your website.Irrrve never study this sort of fantastic content.
free progressive auto insurance quotes car insurance wrote:   
The a real great document. This page is really useful. I would like to recognize another information regarding this kind of posting. Consequently be sure to present other more knowledge about this specific area,Many thanks.
insurance quotes health wrote:   
I truly Enjoyed Your blog post. I've got Just simply Book marked. We're Normal Guest Of Your Web site I am going to Share The item By using My local freinds Appreciate it and that i assures Let me go to your website all over again.
Khyrid wrote:   
"// Put this script on a GameObject with your camera and a character controller on it."

Whats a camera and character controller?
um wrote:   
Read the script (BetaComment)
David wrote:   
I am just Starting out on unity and was wondering where to put this. Thanks for the contribution this will help on my projects. (BetaComment)

Leave a comment

Name:
Website:
Comment:

Email:
Captcha:
Choose the Martini glass image from below:

           

           

 

This site is mostly about

Video Game Design

User Interface Design

Creative & fun stuff

 

Your Host

I'm Hanford Lemoore. My parking skills are unparalleled.

I make things. From consumer electronics, to video games, to theme park attractions. Perhaps I can make things for you! Check out my portfolio.

When I'm not making things for other people, I'm usually experimenting.

 

Contact

Follow me on Twitter.

Message me on Facebook.

Email me using my contact form.

 

RSS 2.0

 

maquettegame.com

tikiroom.com

junkyardclubhouse.com

monolux.com

 

   


Copyright 2012 Hanford Lemoore | Blog | About | Portfolio | Contact
Powered by Olark