• 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 DaPham · Sep 26, 2018 at 06:09 PM · rotatemousepositionaim

Make my Gunpoint aim/rotate on the z axis towards my mouse cursor

Hello community


I'm making a 2.5D sidescrolling game (2d gameplay so only they cannot move on the Z position), and trying to implement that the character can throw an axe in an arc. The arc part is done by adding gravity to the projectile and a certain speed when the projectiles are instantiated.


However, I'm stuck on aiming with the mouse.

I've tried various codes such as the two below:

 public float speed = 5f;
 
 private void Update()
 {
     Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
     float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
     Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward)
 }

and

     void Update()
     {
         Vector3 direction = Input.mousePosition - transform.position;
         Quaternion rotation = Quaternion.LookRotation(direction);
         transform.rotation = rotation;
                     
     }



The closest to remotely make it rotate was just to write the following code:

         Vector3 direction = Input.mousePosition - transform.position;

But doing this doesn't take in consideration of the gamescreen, and it seems it only makes it rotate on the rotation.x axis, and what I desire is it to rotate on the rotation.z axis.

However, this apparently makes my projectile not go towards the direction its supposed to.

In case my projectile-instantiation codes are relevant:

  void Update()
     {
 
         if (timeBtwAttack <= 0)
         {
 
             player = GameObject.Find("Player").GetComponent<Player>();
 
         if (Input.GetKeyDown(KeyCode.Mouse1))
 
         {
             if(player.facingRight == true)
             { 
             GameObject fireballInstance;
             fireballInstance = Instantiate(throwingAxe, spawnPoint.position, spawnPoint.rotation) as GameObject;
             fireballInstance.GetComponent<Rigidbody2D>().AddForce(spawnPoint.right * speed);
             }
             else
             {
                 GameObject fireballInstance;
                 fireballInstance = Instantiate(throwingAxe, spawnPoint.position, Quaternion.Euler(new Vector3(0, 0, 0))) as GameObject;
                 fireballInstance.GetComponent<Rigidbody2D>().AddForce(-spawnPoint.right * speed);
             }
                 timeBtwAttack = startTimeBtwAttack;
 
         }
 
         }
         else
         {
             timeBtwAttack -= Time.deltaTime;
         }
 
     }



Hope somebody can guide me in the right way.

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

Answer by Goest_ · Sep 26, 2018 at 06:55 PM

Ok, so the vector3 you feed in to the ScreenToWorldPoint() method needs to have a z value that represents the distance from the camera to your player. With this in mind you can add to your code and end up with something like this

 void Update()
     {
         //The distance from your player to the camera
         float camToPlayerDist = Vector3.Distance(transform.position, Camera.main.transform.position);
 
         //This is the world position of your mouse
         Vector2 mouseWorldPosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, camToPlayerDist));
 
         //The direction your mouse is pointing in with relation to your player
         Vector2 direction = mouseWorldPosition - (Vector2)transform.position;
 
         //the angle of your direction
         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
 
         //Setting the rotation to the transform.
         transform.rotation = Quaternion.Euler(0, 0, angle);
     }

The transform you add this code to will be doing the rotating so make sure you set the rotation of the "weapon" you want to rotate. In my example I rotate whatever transform this script is attached to. Hope this helps

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

94 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

Related Questions

Cast a cursor into world space from a game object? 0 Answers

Rotate an object accordingly to the angle from object to mouse position 2 Answers

Rotating object with mouse 2 Answers

rotare arrow object to mouse position 0 Answers

How rotate a gameobject in Y axis using the mouse position at screen? 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