- Home /
How to null children of main camera that is also a child?
var start: Transform; // drag here the start empty object
var end: Transform; // drag here the end empty object
var duration: float = 1.6; // set duration of movement
var isNotDead:boolean=true;
var showGameOverScreenLboolean=false;
function Update(){
if(isNotDead){
// other things that you don't want the player to do while dead here
if(health<=0){
isNotDead = false;
KillPlayer();
}
}
}
function DeathCamera(){
var cam: Transform = Camera.main.transform; // get camera transform
cam.parent = null; // detach it from the player
cam.position = start.position; // place the camera at the start position
cam.LookAt(end.position); // make it look towards the end position
var t: float = 0;
while (t < 1){
t += Time.deltaTime/duration;
// move the camera towards the end position each frame
cam.position = Vector3.Lerp(start.position, end.position, t);
yield; // let Unity do other jobs til next frame
}
// movement ended - enable game over screen
showGameOverScreen = true; // enable "Game Over" message in OnGUI
}
function KillPlayer(){
DeathCamera(); // start DeathCamera coroutine
// not sure if the line below effectively stops the player...
GetComponent(CharacterController).enabled=false; //makes the player unable to move
//Other things here - showGameOverScreen is activated by DeathCamera
Screen.showCursor=true;
Screen.lockCursor=false;
}
function OnGUI(){
if(showGameOverScreen){
//GUI code with whatever you want to have at the death screen.
}
}
So I have recently gotten this awesome script. The issue I'm having is that once the camera move towards my Monster's face from death, children of the Main Camera also follow the camera. I have spotlights with a flashlight under the Main Camera, and I'm trying to null the children of the camera, which is also a child of the First Person Control Player. Of course seeing your flashlight randomly flying towards the Monster as you died looks ridiculous. Any help would be great! :)
Answer by DaveA · Sep 18, 2013 at 03:03 AM
What do you mean by 'null the parent'? Setting the transform.parent to null makes it a top-level object. Are you saying you want to 'detach' things that are children of the object the camera is on? If so:
for (var t : Transform in Camera.main.transform)
t.parent = null;
Sorry for not being clear. Yes, I would like to 'detach' the children of the $$anonymous$$ain Camera, which is also a child of the First Person Control Player.
Thank you for your answer that helped me INSTANTLY. This is why members of this community need people like you! :D
Your answer
Follow this Question
Related Questions
Transform.rotation is setting local rotatoin 0 Answers
Avoid scaling GameObject after parenting 2 Answers
Parent component affect child objects 0 Answers
Strange movement artifacts when rotating children around parent object 1 Answer
How to move a gameObject with another gameObject but slower 1 Answer