So the bullet comes out of the gun fine, it just always faces one direction, like north, if i aim west, the front of the bullet aims north.
Video:
public GameObject prefabBullet;
private bool pause;
public float shootForce;
// Use this for initialization
void Start ()
{
pause = false;
}
// Update is called once per frame
void Update ()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
pause = !pause;
}
if(Input.GetButtonDown ("Fire1"))
{
if(!pause)
{
GameObject instanceBullet = Instantiate(prefabBullet, transform.position, prefabBullet.transform.rotation) as GameObject;
instanceBullet.rigidbody.AddForce(transform.forward * shootForce);
}
}
}
Thanks
I think you should change line 23 like this:
23. GameObject instanceBullet = Instantiate(prefabBullet, transform.position, transform.rotation) as GameObject;
So that bullet rotates as transform rotates. The way you wrote it it was facing always the same rotation that is its initial rotation.
Now there is a problem, the prefab it’s self is standing up when I shoot, and I didn’t model it in any external programs, i just shrinked a oval and apply a material, I have tried moving it into scene, rotating it, then creating a new prefab and adding it in removing the old one, but it still faces up, the rotation on the prefab I have change many times, maybe I add something to the update function on a script I have attached to the bullet that forces it’s y to face forward?
You can see the problem here: http://anontakesover.com/Mafia%20Vs%20Townsmen.html
it shoots facing up
if someone could lead me in the right direction, that would be great!
I still have this problem.
anyone?
It doesn’t matter what rotation the prefab has because you change it immediately when you instantiate it. Your problem is that your model is facing the wrong way. You could add the bullet as a child to an empty gameobject (rotated at 0,0,0), rotate it properly, and turn that into a prefab. But that creates an extra gameobject for no reason. It would be better to simply fix your model so that it is facing the right way.
Every 3D program has its own (somewhat arbitrary) definition of what up, forward, and right are. In Unity, along the Z-axis (blue) is forward. In some modelling programs the Z-axis is up and the Y-axis is forwad. If it is different, you have to flip your pivot point in your modelling program so it will be facing the right way in Unity.
Your other option is just to go on the Asset store and find a free bullet model facing the correct direction that you can use. There is probably some example project out there that includes one.