I have problems with fireing, when i fire bullet it isnt go forward but sideway. WHY!?
This is my script…
#pragma strict
var fireballbullet : Transform;
var firesound : AudioClip;
var hadukenbullet : Transform;
var hadukensound : AudioClip;
//Intergers
var weaponnumber = 1; //1 = fireball, 2 = haduken
function Start () {
}
function Update () {
if (Input.GetKeyUp ("e"))
{
if (weaponnumber == 1)
{
var bulletfire = Instantiate (fireballbullet, gameObject.Find("Bullet_SpawnPoint").transform.position,Quaternion.identity);
audio.Stop ();
audio.PlayOneShot(firesound);
bulletfire.rigidbody.AddForce (transform.forward * 2000);
}
if (weaponnumber == 2)
{
var bullethado = Instantiate (hadukenbullet, gameObject.Find("Bullet_SpawnPoint").transform.position,Quaternion.identity);
audio.Stop ();
audio.PlayOneShot(hadukensound);
bullethado.rigidbody.AddForce (transform.forward * 2000);
}
}
if (Input.GetKeyUp ("1"))
{
weaponnumber = 1;
}
if (Input.GetKeyUp ("2"))
{
weaponnumber = 2;
}
}
Things are a bit of a mess here, so they are hard to sort out. First, you are Instantiating() your bullet with Quaternion.Identity, so the rotation of the bullet itself will be arbitrary with respect to the spawner. This might work for a sphere, but not for any other object.
Next, you are using the rotation of whatever this script is attached to for firing, but you are referencing some third object to get the position to fire. If I had to guess I’d say you need to do:
bulletfire.rigidbody.AddForce (GameObject.Find("Bullet_SpawnPoint").transform.forward * 2000);
This assumes that you are aligning the spawn point with the target. Not sure if that is true. And using the rotation of what this script is attached to will only work if it and the spawn point are at the same position…which would then negate the need for a spawn point. Typically firing scrips are attached to the spawn point.
Note is is better to do the GameObject.Find() in start and store the reference (Transform in this case). You don’t want to be doing a Find() at every bullet firing if you don’t need to.