- Home /
Shoot Script
Does anyone know how to write a script that will let you shoot? I already have one but am looking for one that doesn't transform the prefab "forward" as it won't work if the player looks up. Thankyou so much in advance.
What does your script look like? Im not really that great with scripting but when I use "Instantiate" I Instantiate a projectile at a position from a Fire Point and then on the projectile mesh I apply a "constant force" and type in a number in the relative force parameter. That way the projectile will always "fly" in the relative direction form where it was fired.
Hi serenefox, thanks for replying. This is my current script (it's for a mobile platform) - var prefabBullet:Transform; var shootForce:float;
function Update()
{
if (Input.touchCount == 1)
{
var touch: Touch = Input.touches[0];
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position))
{
var instanceBullet = Instantiate(prefabBullet, GameObject.Find("shootspawn").transform.position, GameObject.Find("$$anonymous$$ain Camera").transform.rotation);
instanceBullet.rigidbody.AddForce(GameObject.Find("Player").transform.forward * shootForce);
}
}
}
When i shoot by pressing the button it only does it forward according to the player and when i rotate the camera up it doesn't shoot up. I'm not that good at coding either :D. Thanks again, Johnny
Ok I haven't really done anything mobile yet but it should be the same concept I believe(and anyone can correct me if Im wrong). here is a script I did for something I am working on:
var projectilePrefab : GameObject;
var fp1 : GameObject;
var fireRate : float = 1;
var nextFire : float = 1;
function Update ()
{
if( Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(projectilePrefab, fp1.transform.position, fp1.transform.rotation);
}
}
This would go on my characters gun and in the scene I would create an empty gameobject that would be a child under the gun. The empty gameObject would be placed at the end of my gun mesh(so where the bullet would come out). In the Inspector on the gun I would assign all the correct parts to the variable slots(for example the projectile mesh to the "projectilePrefab" and so on). Then on the projectile mesh I would go to Component>Phsyics>Rigidbody to add a rigidbody(if you want to you can mess with the rigidbody's parameters) because you need a rigidBody to use a constant force. Then go to Component>Phsyics>ConstantForce (then mess with the "relative force" parameter until when you fire your projectile it goes straight out from the gun). The empty gameobject's (the firepoint's) axis is what direction the projectile will go off of so look at the way it is facing for the right axis to change in the constant force.
But that that extent of my knowledge on this. Hopefully you will be able to look at my script example and be able to change yours so it works properly.
The Fire Rate and Next fire parts of my script are there to space out the shots so its not rapid fire constantly when you press fire.
Thankyou so so much. It was exactly what I needed. I really appreciate you Help :D
Answer by serenefox · Mar 17, 2013 at 10:40 PM
var projectilePrefab : GameObject;
var fp1 : GameObject;
var fireRate : float = 1;
var nextFire : float = 1;
function Update ()
{
if( Input.GetButton("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Instantiate(projectilePrefab, fp1.transform.position, fp1.transform.rotation);
}
}
This would go on my characters gun and in the scene I would create an empty gameobject that would be a child under the gun. The empty gameObject would be placed at the end of my gun mesh(so where the bullet would come out). In the Inspector on the gun I would assign all the correct parts to the variable slots(for example the projectile mesh to the "projectilePrefab" and so on). Then on the projectile mesh I would go to Component>Phsyics>Rigidbody to add a rigidbody(if you want to you can mess with the rigidbody's parameters) because you need a rigidBody to use a constant force. Then go to Component>Phsyics>ConstantForce (then mess with the "relative force" parameter until when you fire your projectile it goes straight out from the gun). The empty gameobject's (the firepoint's) axis is what direction the projectile will go off of so look at the way it is facing for the right axis to change in the constant force.
But that that extent of my knowledge on this. Hopefully you will be able to look at my script example and be able to change yours so it works properly.