I'm experiencing a rather annoying bug and I was hoping someone could offer some help because this just isn't making sense to me. I have a Ballistic
script that tells the bullet to move in its local forward direction, which should be whichever way the gun is pointing. I have a PlayerAttack
script that calls for a bullet to spawn with OnShootEnter()
. It sets the bullets position to that of a bulletSpawn
transform, which is a child of the gun.
OnShootEnter()
is being properly called and bulletSpawn
is properly assigned; and yet, when the bullet spawns, its facing Vector3.forward
, instead of it facing bulletSpawn.forward
. These are the only two scripts affecting the bullets spawn, rotation, and direction. Everything looks in order to me; what am I missing?
public class PlayerAttack : MonoBehaviour
{
public GameObject bulletPrefab;
public Transform gun;
public Transform bulletSpawn;
public float damage;
void OnShootEnter()
{
GameObject bullet
= Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
}
}
public class Ballistic : MonoBehaviour
{
public GameObject model;
public float range;
public float accuracy;
public float speed;
Vector3 dir = Vector3.zero;
void Start ()
{
model.transform.SetParent (this.transform);
dir = transform.forward;
}
void Update ()
{
transform.GetComponent<Rigidbody> ().velocity = dir.normalized * speed;
range -= Time.deltaTime;
if (range <= 0)
Destroy (this.gameObject);
}
}