- Home /
Switch camera to cloned object.
Hello again.
I have a problem with my camera, it follows a player like a 3rd person camera, it is fine but when my player dies an instantiated ragdoll is cloned and player object is destroyed. So I want my camera follows that ragdoll. I got this so far:
#pragma strict
var target : Transform;
var height : float;
var distance : float;
var shoulderView : float;
private var targetPosition : Vector3;
function Update(){
if(_playerHealth.playerDead){
var sf : _cameraScript = Camera.main.GetComponent(_cameraScript);
sf.target = transform.Find("Ragdoll(Clone)");
}
}
function LateUpdate () {
if(!_playerHealth.playerDead){
transform.LookAt(aimTarget);
}else{
transform.LookAt(target);
}
targetPosition = target.position + target.right * shoulderView + target.up * height - target.forward * distance;
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 30.0);
}
I've tried things like replacing target with another variable finding the clone with tag etc.. And I have the same error: NullReferenceException at targetPosition variable in LateUpdate.
Thanks in advance.
Thanks, I get the cloned ragdoll on target in my camera, but now I can't get the ragdoll position, even if I put the ragdoll directly in the scene its coordinates don't change. Any idea? any way here's the script I used:
function Update () {
if(_playerHealth.playerDead && !swap){
target = GameObject.FindWithTag("playerDead").transform;
swap = true;
}
targetPosition = target.position + target.right * shoulderView + target.up * height - target.forward * distance;
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 30.0);
transform.LookAt(target);
if (!target) return;
}
Ok, I got it working, about the ragdoll I had to get the ragdoll hips or root position , some like:
targetPosition = target.Find("Root/Hip").position + Vector3 (3.0 , 3.0 , 3.0);
transform.LookAt(target.Find("Root/Hip").position);
Just added that to my player dead condition and the camera can follow the ragdoll well. Thanks for helping.
Fattie, can you answer? so I can mark it as answered.
Answer by Fattie · Sep 09, 2013 at 07:59 AM
Just FYI. you definitely shouldn't be doing this sort of thing in Update. Don't forget, update happens 100s of times every second. if you think about it, it doesn't make sense to check "is player dead" 100s of times a second. you need to swap the camera AT THE TIME the gameobject is destroyed, when it happens.
further, really you just shouldn't use LateUpdate, it's very unnecessary and almost certainly mistaken here. hope it helps a bit!
Your answer
Follow this Question
Related Questions
How to set main camera? 3 Answers
setting a character controller orientation by script 1 Answer
Switching camera effects 1 Answer
Switching Several Cameras(HorrorGame) 1 Answer
Swapping Cameras Disables Keyboard Input 3 Answers