- Home /
camera Lookat FPC problem
Im trying to modify this deathscript so that the camera stops and then looks at the player as they move. I've come as far as getting the Main camera to unparent itself from the FPC, but I can't seem to get it to stay on the player. At the moment all it does is reset its position.
var target : GameObject; // drag here the end empty object
function KillCam(){
var cam: Transform = Camera.main.transform; // get camera transform
cam.parent = null; // detach camera from the player
cam.LookAt(target.transform.position); // make it look at the player
yield;
}
Any help would be greatly appreciated
Why are you un-parenting the camera? Shouldn't need to do so. Just use lookAt.
Well want the camera to stop moving with the player and simply look at them. if I don't unparent it will stay with the player(in first person)
the effect I'm going for is sort of like going from first person to third. so the player will fall ahead of the camera.
Although, even without it I still can't seem to get it working
Answer by RyanZimmerman87 · Feb 06, 2014 at 08:37 AM
I would probably just set up all your camera logic into two sections one for being alive and one for being dead.
For Example:
//this script is on Camera
Transform cameraTransform;
Transform playerTransform;
//speed for camera
float cameraMoveSpeed;
void Start()
{
cameraTransform = transform;
playerTransform = GameObject.Find("Player").transform;
cameraMoveSpeed = 1;
}
void LateUpdate()
{
//easy dead or alive bool access for sake of example
if (PlayerScript.playerAliveBool == true)
{
//all normal camera behavior
}
else if (PlayerScript.playerAliveBool == false)
{
//dead camera behavior
cameraTransform.LookAt(playerTransform.position);
//move backwards at cameraMoveSpeed
cameraTransform.position += -camerTransform.forward * cameraMoveSpeed * Time.deltaTime;
//move up at cameraMoveSpeed
cameraTransform.position += camerTransform.up * cameraMoveSpeed * Time.deltaTime;
}
}
That should move the camera simply upwards and back from the player. If you want something nicer you might want to just create two Transform variable when you die and use lerp to move between the two (one for spot of camera right at death, one at a spot relative to the death spot).
For example:
cameraTransform.position = Vector3.Lerp (deathStartTransform.position, deathEndTransform.position, .1f);
You could also just use vector positions to move around rather than transform.forward or transform.up stuff.
Example:
cameraTransform.position = new Vector3 (cameraTransform.position.x, cameraTransform.position.y +.1f, cameraTransform.position.z - .2f);
I really have no idea how any of this would look so you would need to adjust variables this is basically just syntax examples.
There's really a million different ways to do this type of thing so you just need to find what looks best to you.
This helped immensely! I was able to get a good result by using the edited script with your move camera backwards. Plus I got a little practice converting c# to javascript. $$anonymous$$any Thanks!!
Answer by Nanobrain · Feb 04, 2014 at 06:41 AM
Don't un-parent the camera. Instead, I would suggest just moving the camera along it's local axises away from the player along with lookAt.
I'm trying to go with what you said but I'm not sure what I'm doing wrong. I looked around and made this script that does get the cam to look at whatever I choose, but it wont move the cam at all after I tried to add the translate part.
var target : Transform;
var speed = 2.0;
var smooth = 5;
cam = Camera.main;
function Awake(){
var script3 = GetComponent("DeathScript");
script3.enabled = false;
}
function LateUpdate(){
var cam1 = Camera.main.transform;
cam1.Translate(Vector3.-forward);
var quaterion= Quaternion.LookRotation(target.position - cam1.position);
transform.rotation = Quaternion.Slerp(transform.rotation, quaterion, speed * 0.02);
}