Hey all,
I’m having trouble with the math involved in getting a projectile to fire at a constant speed in the direction a game object is facing. Like shooting a turret.
I can’t seem to get it to work. Not sure what to work with either eg degrees or radians. Here is what I have tried.
GameObject bullet = (GameObject) Instantiate(bulletPrefab, nozzel.transform.position, Quaternion.identity);
float turretAngle = turret.transform.localRotation.z;
float bulletAngle = (turretAngle * 180 / Mathf.PI);
float bulletSpeed = 2;
float ax = Mathf.Cos(bulletAngle) * bulletSpeed;
float ay = Mathf.Sin(bulletAngle) * bulletSpeed;
Vector3 bulletForce = new Vector3(ax,ay,0);
bullet.rigidbody.velocity = bulletForce;
It seems to move at the correct speed just not the correct direction. Also I have setup my turret sprite a little weird I need to subtract 90 degrees of the angle.
EDIT: Also I just had a quick thought to I need to multiply something by time.deltatime ? so that the slower fps moves correctly?
Thanks for any help