- Home /
Instantiate a projectile on key press "F" key / OnMouseDown! Help!!!
Okay so again I'm stuck, I want a prefab of a sphere that emits which I have created to get fired/(instantiated) out from the player_character I have when either I click the "left mouse button" or hit the "F" key. The Sphere is tagged as "PlayerProjectile". I'm assuming it'd be something on the function Update like function Update() { if(Input.GetAxis("Fire1") >.1) { instantiate."player projectile"; } } I know that's the wrong way it'd be written I just need a little help. Thanks!!! Also if anyone could throw it I have an enemy cube with a health GUI display and a health variable and i need a way in that enemy's health script to detect if its been hit by one of these PlayerProjectiles and lose health accordingly. The Script on the Enemy Cube is - var hp:float; var maxhp:float; var healthbarWidth:int; var myHealthbar:GameObject; var myhb:GameObject;
function Start(){ healthbarWidth=20; myhb=Instantiate(myHealthbar,transform.position,transform.rotation); }
function Update(){
myhb.transform.position=Camera.main.WorldToViewportPoint (transform.position);
myhb.transform.position.x-=.05;
myhb.transform.position.y-=.05;
myhb.transform.localScale=Vector3.zero;
var healthpercent:float=hp/maxhp;
if(healthpercent<0){healthpercent=0;}
if(healthpercent>100){healthpercent=100;}
healthbarWidth=healthpercent*20;
myhb.guiTexture.pixelInset=Rect(20,20,healthbarWidth,10);
} Of course that part won't do me any good until i can actually fire the projectiles. Thanks for any help in advance. : )
Answer by Piflik · Sep 26, 2011 at 07:49 PM
if(Input.GetKeyDown(KeyCode.F) || Input.GetKeyDown(KeyCode.Mouse0))
Instantiate(Prefab, transform.position, Quaternion.identity);
Format your code correctly next time, please. It is hard to read as it is now.
Answer by RaiderA528 · Oct 04, 2011 at 01:10 PM
Sorry it messed up when I posted it. it was indented and everything in the text box when i typed it in! : s
Also select @Piflik's answer as the correct one, if he helped you! :) The question will be marked as "solved" in this way.