Hi,
Here’s my code of shooting via a joystick, the bullet shoots in the right direction, faces the camera fine, BUT doesn’t rotate around the Z (right axis?) so the points in the direction it should ;(
Vector3 shootDirection = new Vector3(Input.GetAxis("FireHorizontal"),Input.GetAxis("FireVertical") * -1f, 0).normalized;
Vector3 _fireFromHere = fireFromHere.transform.position;
// create bullet
GameObject bulletInstance = Instantiate(bullet,_fireFromHere,Quaternion.identity) as GameObject; // wrong angle
// face direction ready to shoot....
float angle = Vector3.Angle(Vector3.zero, shootDirection);
Quaternion rot = Quaternion.AngleAxis(angle, Vector3.forward);
bulletInstance.transform.rotation = rot;
Debug.Log("shootDirection: " + shootDirection + " | angle:" + angle + " | rot: " + rot);
// ....shoot!
bulletInstance.rigidbody2D.AddForce(shootDirection*bulletSpeed);
I’ve tried Vector3.forward/up/right but honestly don’t really understand the maths behind so would appreciate a hand.
Extra points for explaining how it the maths DOES work ![]()
Cheers,
Andy