- Home /
bullet prefab always come out in wrong direction
i created a cube with rigidbody as a bullet prefab but then the bullet prefab always come out in wrong direction what could be wrong? please help
heres my code:
var prefabBullet:Transform;
var shootForce:float;
var shots : int = 0;
var maxShots : int = 8;
var shootSound : AudioClip;
function Update()
{
if(Input.GetButtonDown("Fire1") && shots < maxShots)
{
var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity);
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
Destroy(instanceBullet.gameObject, 5);
audio.PlayOneShot(shootSound);
animation.Play("gunidle");
shots++;
}
else if (shots >= maxShots && Input.GetKeyDown(KeyCode.R))
{
animation.Play("gunshots");
shots = 0;
}
}
Which direction does it go in? Is it always to a certain point in the scence of always going to the left/right ect
What is the "wrong" direction. It looks like you are always firing it towards the positive Z axis.
CeejayZSmith - forward/backward
robertbu - because in the rigidbody of the bullet i freeze all the rotation and in position i freeze only the x and y
What we need to understand is what you want to happen. Your code shoots the bullet down the positive Z axis. Did you want it to shoot towards the negative Z axis or somewhere else?
i want it to be like a real gun... at any direction i point the gun the bullet will come out
Answer by FlammingFart · Feb 21, 2013 at 07:20 PM
in the cube which is the bullet prefab... i put a rigidbody and i freeze its position x and y not the "z" as well as the rotation x, y and z
but then the bullet prefab came out either forward and backward only
Answer by robertbu · Feb 21, 2013 at 07:31 PM
If you are just trying to make a bullet come out of gun, I suggest.
Don't freeze the x and y movements in your prefab.
Put an empty game object just in front of the barrel of the gun. This will be the spawn point for the bullets and (I'll call it goSpawn).
Rotate the empty object so that it's rotation matches the barrel of the gun. Note the positive Z direction is forward.
Make the empty game object a child of the gun by dragging it on top of the gun in the Hierarchy view.
When calling Instantiate, use the position and rotation of goSpawn.
var instanceBullet = Instantiate(prefabBullet, goSpawn.transform.position, goSpawn.transform.rotation); instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
what about the goSpawn? theres an error about the unknown identifier goSpawn
goSpawn is the spawn point...the empty game object. If you attach the bullet spawning script to the spawn point, you can get its values from "transform.position" and "transform.rotation" rather than goSpawn. If you are spawning from another script, you will need to declare the variable and initialize it somehow...GameObject.Fing() or by dragging a game object in the inspector.
This is really not helpful @Dustin111 In general the process described in the answer is a good way to approach this issue.
If you think something in there is not correct then please rather add a suggestion of what someone could do better.
If you face a problem with your setup in your own project then please open a new question with a detailed description of your current setup.
Your answer
Follow this Question
Related Questions
Best way to make a gun? 1 Answer
how to make my gun not fire while paused 2 Answers
How to add Bullet Velocity 1 Answer
How can I can I cast a ray from a gameobject? 1 Answer
how do i make a gun 1 Answer