- Home /
The question is answered, right answer was accepted
XBox Controller Angle of Fire Incorrect
I am using an XBox controller, right analog stick to fire a projectile in a 2D game, and using the right analog stick to create the angle that it fires. The analog stick reports the angles correctly, but it seems I'm interpreting the angle to always be off (by about 70 degrees counterclockwise, it seems).
I checked other suggestions on how to find the correct angle, but I can't figure out how I'm using that angle incorrectly. If you have any other advice for this code to make it better, feel free to commend other suggestions. Everything helps!
var xAim = Input.GetAxis("R_XAxis_1");
var yAim = Input.GetAxis("R_YAxis_1");
// Calculate the angle of the thumbstick
var targetAngle : float = Mathf.Atan2(xAim, yAim) * Mathf.Rad2Deg;
//Debug.Log(targetAngle);
var targetVector = Vector3(0, 0, targetAngle);
if( Input.GetAxis("Triggers_1") > .5 && (allowFire)){
Fire(targetVector);
}
}
function Fire(targetVector){
allowFire = false;
var shot = projectile.Instantiate( projectile, transform.position, Quaternion.identity );
//make sure player can't hit himself with it
Physics2D.IgnoreCollision( shot.collider2D, collider2D );
//turn to face where it's heading
shot.transform.Rotate( targetVector );
//Destroy automatically after time
Destroy(shot.gameObject, 2);
yield WaitForSeconds(.3);
allowFire = true;
}
One problem is that you have the parameters to your $$anonymous$$athf.Atan2() reversed. It should be:
var targetAngle : float = $$anonymous$$athf.Atan2(yAim, xAim) * $$anonymous$$athf.Rad2Deg;
Note sure how you are interpreting everything, but Vector3.right is 0 angle, so holding the joystick to the right will give back 0. Holding the joystick up will give 90.0.
For it to work exactly right, I actually needed to pass in the negative of yAim. I'm not sure why that is, but otherwise it works great, thanks!
Follow this Question
Related Questions
what to put in vector3 when instantiating Gameobject at Gameobject? 2 Answers
How to instantiate a new GameObject (sprite) properly? 1 Answer
Instantiating a gameObject in an array is not working for me 1 Answer
Checking if a gameobjects active in your heirarchy. 2 Answers
Is there any way i can Instantiate a gameobject into an Instantiated gameobject as a child? 1 Answer