Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 29, 2014 at 08:19 PM by tanoshimi for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by EClancy · Aug 26, 2014 at 01:16 AM · 2dgameobjectinstantiatevector3controller

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;
 }
 
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Aug 26, 2014 at 01:42 AM 0
Share

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.

avatar image EClancy · Aug 27, 2014 at 02:12 AM 0
Share

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!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges