- Home /
Key press hides object and spawns another
I have a playable character, I have it set to that on a key press he damages an enemy, but what I want is a script that will hide my character for 3 seconds and spawn an object in it's place and then after the 3 seconds destroy the spawned object and unhide the character. Any ideas?
function Update () {
gameObject"spinny".active = false;
if (Input.GetKey ("fire1") && Time.1 >)
gameObject"spinny".active = true;
}
Answer by Statement · Mar 03, 2011 at 01:54 PM
I guess you could do something similar to this (but make sure to read Joshuas links, not all will be free):
// Set this to the prefab you want to create. var inPlacePrefab : GameObject;
function Start() { // Thanks to Derek that pointed out we can't use Update. // The code has been revised, now we use another Update routine. StartCoroutine(UpdateInput()); }
function UpdateInput() { while (true) { if (Input.GetButton("Fire1")) { yield HideAndSpawn(); } else { yield; } } }
function HideAndSpawn() { // 1. Hide self with disabling renderer. renderer.enabled = false;
// 2. Spawn that prefab in our place.
var spawned = Instantiate(inPlacePrefab, transform.position,
transform.rotation);
// 3. Wait 3 seconds.
yield WaitForSeconds(3);
// 4. Destroy the spawned item.
Destroy(spawned);
// 5. Show self with enabling renderer.
renderer.enabled = true;
}
Wouldn't that yield in Update give you the old "Update cannot be a coroutine"? Also even if it did work it would start a new HideAndSpawn() coroutine every frame you had Fire1 depressed, wouldn't it?
I created another method UpdateInput for this task. Putting the whole update loop in a coroutine will stop the code in HideAndSpawn until it is completed, so it can't launch several HideAndSpawn running in parallel.
$$anonymous$$ake sure to see my updated answer, I haven't tested it myself (shame on me) but to me it looks like it would work now :)
Yeah it works, i'll edit it a bit to hid all of the rendered meshes in my main character, and some how make it move with my character :S, much appreciated
Answer by Joshua · Mar 02, 2011 at 08:36 PM
Just break it down into simple things. I'm pretty sure every step of the way has already been answered somewhere on this site.
How to execute a command on pressing a key.,
How to make something invisible.,
How to instantiate an object.,
How to make something happen when timer reaches a certain point.
How to make something invisible visible again..
If there's a specific step you're having trouble with I'd be more then happy to help, that's what this site is here for. But we're not here to write free scripts for you.
Edit: when I wrote this the original question was a LOT less detailed, so ignore the tone of the message :) not deleting the actually post because the links might still be useful.
Sorry was in a bit of a hurry when posting, I've updated the first post with my script so far, it's not working though :S
No, we're not here to write free scripts, but having both approaches doesn't maim :)
I commented on Joshuas remark "But we're not here to write free scripts for you.", I didn't reply you.
Heh Statement, when I posted this the question was a little less.. explicit so you may have gotten the wrong impression when you read it. ;) kudos on writing the script though, def +1 from me.
Your answer
Follow this Question
Related Questions
How to get raycast and key click info. 1 Answer
Rotating an Object on Key Press! 3 Answers
Rotate on Key press help? 1 Answer
Hide GUI Label when click in open space or on other objects 1 Answer
Spawning if there is space 1 Answer