Evening, all.
I am creating a 2D platformer game in which the player shoots in the direction of where the mouse is pointing at. I am trying to set the rotation of an instantiated bullet prefab in the direction of where the mouse is pointing. The bullet will correctly travel at _deltaX in the X direction, and _deltaY in the Y direction, but will NOT be instantiated in the direction that I specify.
This ShootController.cs class is attached to the Player. The Player does have their RigidBody rotations frozen though.
Any advice would be greatly appreciated!
public class ShootController : MonoBehaviour {
public GameObject bullet;
GameObject playerObject;
//Attributes for finding the Angle of Attack
private float _deltaX;
private float _deltaY;
private float _radians;
private float _degrees;
void Start ()
{
// Assigns the player position values
playerPosX = Screen.width * 0.5f;
playerPosY = Screen.height * 0.5f - 50.0f; // This is to properly align the origin
}
void Update ()
{
// When the PLAYER clicks and Shoots
if (Input.GetMouseButtonDown(0))
{
_deltaX = Input.mousePosition.x - playerPosX;
_deltaY = Input.mousePosition.y - playerPosY;
ShootNormalShot();
}
}
void ShootNormalShot(){
_radians = Mathf.Atan(_deltaY/_deltaX);
_degrees = _radians * Mathf.Rad2Deg;
Vector3 angle = new Vector3(0f,0f, _degrees);
GameObject projectile = (GameObject)GameObject.Instantiate(bullet, playerObject.transform.position, Quaternion.Euler(angle));
projectile.GetComponent().Initialize(_deltaX, _deltaY, _normalDamage, _MaxSpeed);
}
}