- Home /
Character Contolller Hit
I am trying my best to have my object which this code has been attached to react with a simple plane which has been tagged with the name "pann" , when my object touches pann it should respawn and generate a GUI window saying "you died"
Here is my code
var speed =4.0; var rotateSpeed =4.0; var dicePrefab : Transform ; // TRANSFORM private var panel = false; function OnControllerColliderHit (hit: ControllerColliderHit) {
if(hit.gameObject.tag == "pann") { panel= true; }
}
function Update () { var controller : CharacterController = GetComponent (CharacterController); transform.Rotate (0, Input.GetAxis ("Horizontal") rotateSpeed , 0); var forward =transform. TransformDirection (Vector3 .forward); var curSpeed =speed Input.GetAxis ("Vertical"); controller.SimpleMove (forward * curSpeed);
}
function LateUpdate ()
if (panel)
{
transform.position = Vector3 (80,50,0);
// gameObject.Find()
// I tried putting a gui box here / OnGUI doesnt seem to work anywhere in this code.
panel = false ;
}
}
Answer by kievar1983 · Jan 01, 2012 at 10:00 PM
to generate a gui window you actually need to call it through
void OnGUI ()
your best bet is most likely to store some boolean value and set it when the collision is true then do an if statement for the onGUI.
Answer by nastasache · Jan 01, 2012 at 10:27 PM
It's apparently not working with your onGUI() function just because the box with text "you died" immediately disappearing.
Try:
function OnGUI ()
if (panel)
{
transform.position = Vector3 (80,50,0);
// gameObject.Find()
// I tried putting a gui box here / OnGUI doesnt seem to work anywhere in this code.
///panel = false ; /// Here's the point; make it false in other way/place
}
}
Answer by aldonaletto · Jan 01, 2012 at 11:49 PM
You should use a float timer variable instead of the boolean panel, because the box must stay in screen during some time, then disappear and move the character to the respawn position. Change your code to this:
var speed =4.0; var rotateSpeed =4.0; var dicePrefab : Transform ; // TRANSFORM
function Update () { var controller : CharacterController = GetComponent (CharacterController); transform.Rotate (0, Input.GetAxis ("Horizontal") rotateSpeed , 0); var forward = transform.TransformDirection (Vector3.forward); var curSpeed = speed Input.GetAxis("Vertical"); controller.SimpleMove (forward * curSpeed); }
private var timer: float = 0;
function OnControllerColliderHit (hit: ControllerColliderHit){ if(hit.gameObject.tag == "pann"){ timer = 3; // load the timer with 3 seconds } }
function OnGUI(){ if (timer > 0){ // if timer not zeroed, display the box var r = Rect((Screen.width-210)/2, (Screen.height-80)/2, 210, 80); GUI.Box(r, "You're dead!"); } }
function LateUpdate(){ if (timer > 0){ // if timer not zeroed yet... timer -= Time.deltaTime; // decrement timer... if (timer
Your answer
![](https://koobas.hobune.stream/wayback/20220613062559im_/https://answers.unity.com/themes/thub/images/avi.jpg)