Scripting is my Arch-nemesis!

Ok I have tried over and over again to grasp how to script. I figured this would be an easy thing to accomplish. And for the record this is my first real attempt at scripting.

What I want my script to do is make the camera follow an object from a set distance. But I also want it to rotate around the axis in which the object is traveling. For example “Object” is traveling along the Z axis and the camera is following him from 3 units behind him. The camera is rotating around the z axis to seems as though the world is turning. With that here is my script. The following the object works, but the rotation does not. Please help in anyway you can. Thanks

//Camera follow target from distance

var distance = 3;
var target : Transform;



function Update (){
	//Camera position relative to Target
	transform.position = -Vector3.forward + target.position;
	
	//Rotation of Camera 
	transform.Rotate(Vector3.forward * Time.deltaTime, Space.Self);
	
	//What camera is looking at
	transform.LookAt(target);
	
	
}

NA

what you’re describing sounds like the ‘smooth follow’ script that comes with unity, did you try that? (its in the standard assets - camera scripts folder.

I tried that and it doesn’t do what I want. Smooth Follow is a strange script that just seems to confuse me. What I want is a lock camera view, but for it just to rotate on a single axis. This is only for a specific area of my level.

NA

Here is what this code actually does:

var distance = 3;
var target : Transform;

// called every frame
function Update (){

   //make the camera be at the same position as the target, but one unit back in world space.
   transform.position = -Vector3.forward + target.position;
   
   //Rotate the camera along the local forward axis 1 degree per second (Do a barrel roll!)
   transform.Rotate(Vector3.forward * Time.deltaTime, Space.Self);
   
   //Rotate the camera so that it's forward axis points at the target and its up axis points as near to upwards as possible
   transform.LookAt(target);
}

This is what you likely want:

var distance = 3;
var target : Transform;

// called every frame
function Update (){

   //make the camera be "distance" units behind the object that it is following.
   transform.position = target.position - (target.forward * distance);
   
   //Rotate the camera so that it's forward axis points at the target and its up axis points as near to upwards as possible
   transform.LookAt(target);
}

No, that doesn’t work. The descriptions that you changed on my script I wrote is exactly what I want it to do. The camera to follow the player and do a barrel roll. I can get it to follow but not to do the barrel roll.

NA

The problem is that the look function (function might not be the right word) resets the rotation of the object to face the target. When you run the script the object starts to do a barrel roll but then the look function resets the rotation so your object is not doing a barrel roll anymore. So what you can do is make an empty parent for the camera and have that look at the target and have the camera spin. It would look something like this:

This is an empty object which is the parent of the camera and has this script (This is Yoggy’s code).

var distance = 3; 
var target : Transform; 

// called every frame 
function Update () { 

	//make the camera be "distance" units behind the object that it is following. 
	transform.position = target.position - (target.forward * distance); 

	//Rotate the camera so that it's forward axis points at the target and its up axis points as near to upwards as possible 
	transform.LookAt(target); 
}

This is the camera and is a child of the object above and it has this script.

var spin = 100;

function Update () {
	transform.Rotate(0, 0, spin*Time.deltaTime);
}

There is probably a better way of doing this but it should solve your problem.