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 /
avatar image
0
Question by falconstrike209 · Sep 03, 2021 at 07:09 PM · gameobjectinputcontrollermousepositiontransform.rotate

How do I rotate an object using Vector2 inputs? (New Input System)

I have a dash ability in my game that causes the player to dash towards the mouse or the direction the controller knob is being pressed. That all works fine. I want to rotate an arrow-like game object using the input to indicate where the player will dash. I already have a reference to the game object:

 public GameObject dashAim;

But i'm not sure how to use the inputs for the rotation. I've had help with this before, but this was using the old input system:

 public class RotateToMouse : MonoBehaviour
  {
      void Update()
      {
          //rotation
          Vector3 mousePos = Input.mousePosition;
          mousePos.z = 5.23f;
  
          Vector3 objectPos = Camera.main.WorldToScreenPoint(transform.position);
          mousePos.x = mousePos.x - objectPos.x;
          mousePos.y = mousePos.y - objectPos.y;
  
          float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
          transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
      }
  }

i'm just not quite sure how to repurpose this with the new inputs. I already tried this:

 dashAim.transform.rotation = Quaternion.Euler(new Vector3(direction.x, direction.y));

but it didn't do anything. Does anyone know how I can do this? Here's the code i'm using for the inputs, thanks to @Hellium

 // To be called instead of the Dash function when adding the listener
     public void ReadMouseInput(InputAction.CallbackContext context)
     {
 
         Vector2 mousePosition = context.ReadValue<Vector2>();
         Vector3 mousePosition3 = mousePosition;
         Vector3 direction = (mousePosition3 - camera.WorldToScreenPoint(rb.transform.position)).normalized;
 
         Dash(new Vector2(direction.x, direction.y));
     }
     // Callback of the Left Stick
     public void ReadStick(InputAction.CallbackContext context)
     {
         Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
 
         Dash(new Vector2(stickDirection.x, stickDirection.y));
     }

I'd really appreciate any help with this. Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Sep 03, 2021 at 07:41 PM

You might be able to do something similar to how you handle dash.

 public class RotateToMouse : MonoBehaviour
 {
     void Start()
     {
         // Add the listeners to the actions
     }

     void Update()
     {
         // Not needed anymore
     }

     public void ReadMouseInput(InputAction.CallbackContext context)
     {
         Vector2 mousePosition = context.ReadValue<Vector2>();
         Vector2 objectPosition = (Vector2) Camera.main.WorldToScreenPoint(transform.position);
         Vector2 direction = (mousePosition - objectPosition).normalized;
 
         RotateAim(direction);
     }

     public void ReadStick(InputAction.CallbackContext context)
     {
         Vector2 stickDirection = context.ReadValue<Vector2>().normalized;
 
         RotateAim(stickDirection);
     }

     public void RotateAim(Vector2 direction)
     {  
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
     }
 }
Comment
Add comment · Show 1 · Share
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 falconstrike209 · Sep 04, 2021 at 01:20 PM 0
Share

worked great! Only issue was that it was rotating the whole player at first, but I got it sorted out by changing

 transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

to

 dashAim.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

217 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding an offset to instantiated object based on mouse distance from player 1 Answer

How do I use controller knob input like mouse position? (New Input System) 1 Answer

How do I use an Input axis instead of Input.mousePosition??? 1 Answer

How can I stop getting mouse position of Ui Elements? 0 Answers

Input System: Input Action "started" Callback doesn't work properly on controller triggers? 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