I have the secondary camera in the heirarchy and I want it to follow an instantiated object from the instantiated object’s script. I instantiate an object. From within the object’s script I would like to have the secondary camera follow the instantiated object. I will switch back to main camera when instantiated object is destroyed. I am probably looking at this relationship incorrectly. Because the target object is not in the heiarchy with the secondary camera I can not just drag items around to parent or child. If I instantiate the camera I would still be at this point of code-fusion. Can anybody help? I have no code to paste because all attempts would fill up this server. LOL. And I believe the question is simple enough about objects as to gain a few lines of code in return instead of anybody trying to FIX my code. I don’t want to bias any thinking. The secondary camera does rotate around and switch postions but I can see that it competes between the new script and the smoothfollow script. This is because I do not have any camera commands in the object’s script. I see alot of posts about this but I am talking to the camera from the instantiated object’s script. Or so this is truly what I want, i think.
Thanks in advance. I hope the question and explaination is clear enough.
P.S. I would like to get my points up high enough to be able to answer other Unitier’s questions.
“Follow” is not a very clear definition of the movement you want. Follow from behind? Follow from the side? Smoothed follow? The code below follows from directly behind the object without smoothing. It goes on the Instantiated object. There can only be one object in existence at any one time that has this script. It assume the camera game object has the name “SecondaryCamera”. Change this string to whatever you’ve named your secondary camera.
var dist : float = 8.0; // Distance behind to follow
private var secCam : Transform;
function Start() {
secCam = GameObject.Find("SecondaryCamera").transform;
}
function LateUpdate() {
secCam.position = transform.position - transform.forward * dist;
secCam.LookAt(transform);
}
This is what I finally arrived at based upon web search. @robertu, I will use what you posted also for the distance part. I wanted the instantiated object to pull the camera along instead of the camera attaching itself to a found object. Not sure about the localposition or locarotation though. I will have to look into this.
@script AddComponentMenu("Camera-Control/SmoothFollow")
static var pulsarvectors : Transform;
static var PC : Camera;
function Start() {
PC = GameObject.Find("PulsarCamera").camera;
PC.transform.parent = GameObject.Find("Pulsar1").transform; //This puts the Camera as child with Pulsar1 parent!!!
PC.transform.LookAt(GameObject.Find("Pulsar1").transform);
}
function Update () {
// create a normalized vector in the rotation direction
PC.transform.position = transform.localPosition;
PC.transform.rotation = transform.localRotation;