Hello there,
I have a problem with my bullets projectile. I used instantiate to call the bullet at the gun muzzle and fire the bullet when it is instantiate. There is no problem with the instantiate and firing the bullet, all went well.
but as soon as i rotate my gun upward or downward the bullet will fire not according to its rotation, it will still fire straight and not follow the rotation of my gun. why is this happening.
below is my instantiate code:
public int health;
public int currentHealth;
public int playerDamage;
public int bulletScore;
public int endGame;
public GameObject player1;
public GameObject player2;
public GameObject enemyType;
public Transform leftArm;
public Transform rightArm;
public Transform sgBoss;
public Transform mgBoss;
//Quaternion newQuaternion;
Quaternion rotation;
public float rotationSpeed;
public bool canLock;
public bool canShoot;
public int shoot;
public int sgAmount;
float timeBeforeShoot;
float delay;
int bulletCount;
bool ok;
bool ok2;
bool ok3;
// Use this for initialization
void Awake ()
{
ok = true;
ok2 = true;
ok3 = true;
//enemyType = transform.gameObject;
player1 = GameObject.FindWithTag ("fighterOne");
player2 = GameObject.FindWithTag ("fighterTwo");
//newQuaternion = Quaternion.Inverse (Quaternion.LookRotation (Vector3.forward, Vector3.up));
endGame = 0;
health = currentHealth;
}
if (canShoot)
{
timeBeforeShoot += Time.deltaTime;
if (timeBeforeShoot > 3)
{
switch (shoot)
{
case 1:
delay += Time.deltaTime;
if (delay > 0.05f && ok2)
{
Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), mgBoss.rotation);
bulletCount += 1;
delay = 0.0f;
}
if (bulletCount >= 5)
{
ok2 = false;
if (delay > 1)
{
shoot += 1;
ok2 = true;
bulletCount = 0;
delay = 0.0f;
}
}
break;
case 2:
delay += Time.deltaTime;
if (delay > 0.5f && ok3)
{
Transform bullet;
int i;
for (i = 0; i < sgAmount; i++)
{
bullet = Instantiate (sgBoss, leftArm.position, leftArm.rotation) as Transform;
bullet.eulerAngles = new Vector3 (0, 0, -20 + (50 / sgAmount * i));
}
bulletCount += 1;
delay = 0.0f;
}
if (bulletCount >= 1)
{
ok3 = false;
if (delay > 1)
{
shoot = 0;
ok3 = true;
bulletCount = 0;
delay = 0.0f;
timeBeforeShoot = 0.0f;
}
}
break;
default:
delay += Time.deltaTime;
if (delay > 0.05f && ok)
{
Instantiate (mgBoss, new Vector3 (rightArm.position.x, rightArm.position.y, rightArm.position.z), mgBoss.rotation);
bulletCount += 1;
delay = 0.0f;
}
if (bulletCount >= 5)
{
ok = false;
if (delay > 1)
{
shoot += 1;
ok = true;
bulletCount = 0;
delay = 0.0f;
}
}
break;
}
}
}
and this is my bullet projectile when fired:
using UnityEngine;
using System.Collections;
public class BulletsBoss : MonoBehaviour {
public float speed;
public float bulletY;
public bool mgBoss;
public bool doneFire;
// Use this for initialization
void Start ()
{
doneFire = false;
}
// Update is called once per frame
void Update ()
{
transform.Translate (speed * Time.deltaTime, 0.0f, 0.0f, Space.Self);
if (mgBoss && !doneFire)
{
transform.Translate (0.0f, Random.Range (-bulletY, bulletY), 0.0f, Space.Self);
doneFire = true;
}
}
}