- Home /
Firing at a target
I'm making a catapult game, and I want to be able to fire a projectile at a selected area. These are my scripts --- what do I need to do? On camera :
function Update () {
var horiz : float = Input.GetAxis("Horizontal");
transform.Translate(Vector3(horiz,0,0));
}
On Projectile maker (child of camera)
var projectile : GameObject; var ammo : float = 1;
function Update (){ if(Input.GetButtonUp("Jump") && ammo >0){ print ("New projectile!"); var instance : GameObject = Instantiate(projectile, transform.position, transform.rotation);
ammo--; } }
And on projectile collsion:
var explosion : GameObject;
function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.name == "Cube"){ Destroy(gameObject.Find("projectile"), 0); var instance : GameObject = Instantiate(explosion, transform.position, transform.rotation);
} }
Currently a new projectile spawns where the camera is, but if the camera moves the projectile does not. The first projectile does move however (it's a child of the camera.)
Thanks,
Muzz
Justin -> Please use the 101010 tag at top to code-ify your code. Thanks!
When you say the projectile doesn't move, what do you mean? As you've written it, there's no force or velocity on the projectile so it will just drop to the ground. Is that the problem?
Sorry...a few more things. I had a script so that when you pressed space, it fired the projectile (applied a force). Anyway, I've solved it now, with what seems to be some painfully inefficient coding. Never $$anonymous$$d... at least it works!
Answer by Muzz 1 · Mar 03, 2011 at 05:48 PM
I solved it by adding the same movement of camera script to the projectile. Works fine now.