- Home /
Gun shooting help
I am creating a game where you are the starting as the unity jet object. There will be other planes that are enemies that are going to follow the player and shoot him. Right now I am trying to make the player shoot. Right now it instantiaites a yellow cube (tracer fire) and then the cube just falls. It goes forward but not nearly as fast as i want it to be. Also I don't want it to drop, heres my code var Bullet : GameObject; function Update () { %|-829671820_1|% %|52666472_2|% %|1809783453_3|% %|-1054893234_4|% %|1058131029_5|% %|-490803135_6|% // object's Z axis %|-112405093_8|% %|-1590855618_9|% }
Can you repost your code so I can better read it?
Also, you can get an object's z axis a lot more easily with transform.forward if that's what you're trying to do.
var Bullet : GameObject;
function Update () {
// Ctrl was pressed, launch a projectile
if (Input.GetButton("Fire2")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : GameObject;
clone = Instantiate(Bullet, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward);
}
}
Answer by phxvyper · May 17, 2016 at 09:29 PM
Ensure that the Bullet gameobject prefab doesn't have any gravity-applying components such as a Rigidbody. If it does, then disable gravity on that prefab in the inspector:
Also, ensure that you're using the correct forward component:
Change
clone.velocity = transform.TransformDirection (Vector3.forward);
to
clone.velocity = transform.TransformDirection (transform.forward);
and that SHOULD fix the issue.
i dont have any rigid bodies, and yet they still fall. also it seems like the speed at which they go forward is VERY slow. i would like to have an editable speed variable
Answer by daleth90 · May 18, 2016 at 05:31 AM
First, as @phxvyper said, add a RigidBody to your bullet and cancel UseGravity. Your script should have encountered error since you can only control the velocity through RigidBody.
Second, you should check what the value of
transform.TransformDirection (Vector3.forward)
is. Actually, it's (0, 0, 1). So small, right? This is why your bullet move slowly.
OK, so here's the script that should work.
[SerializeField]
private Rigidbody cube;
[SerializeField]
private float speed = 100f;
private void Update () {
if ( Input.GetKeyDown( KeyCode.Space ) ) {
Rigidbody clone = Instantiate( cube );
cube.transform.localPosition = Vector3.zero;
cube.velocity = cube.transform.forward * speed;
}
}
7 errors from this code
[SerializeField]
private Rigidbody cube;
[SerializeField]
private float speed = 100f;
private void Update () {
if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Space ) ) {
Rigidbody clone = Instantiate( cube );
cube.transform.localPosition = Vector3.zero;
cube.velocity = cube.transform.forward * speed;
}
}
Assets/Scripts/GunFire.js(1,18): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/GunFire.js(2,10): BCE0043: Unexpected token: Rigidbody Assets/Scripts/GunFire.js(3,18): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/GunFire.js(4,10): BCE0043: Unexpected token: float. Assets/Scripts/GunFire.js(6,10): BCE0043: Unexpected token: void. Assets/Scripts/GunFire.js(6,24): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/GunFire.js(8,19): UCE0001: ';' expected. Insert a semicolon at the end.
Well, that's because you just "copy and paste".
The main reason is the language I use is C#, not JavaScript, since I don't know anything about JavaScript. :(
You need to try to realize my code and rewrite into JavaScript.
Your answer
Follow this Question
Related Questions
Cannot remove sprite using destroy 1 Answer
Can Any Body Help me how to setup jenkins with unity3d 5.3.4 for automatic builds???? 0 Answers
Script sets color to multiple objects from instead of only one object with Random.range() 0 Answers
Animation depending on the Int value 0 Answers
Unable to enable or disable NavMeshAgent through script? 0 Answers