Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 umbralogian · Apr 28, 2020 at 08:10 PM · rotation3dlookatmouseposition

Rotating a 3D object on the Y axis relative to the cursor's position?

I'm trying to combine the implied camera perspective of classic 2D Zelda titles with the controls of twin-stick shooters, in 3D. I can move the player with the WASD keys, but I'm not sure how to rotate him to face the mouse cursor. I think I need to do the following:

  1. Calculate the cursor's position on the screen.

  2. Draw a line between that position and the player character.

  3. Calculate the difference between the player character's current Y axis rotation and that line.

  4. Rotate the player character on the Y axis by that difference, so that the difference becomes 0.

This way if the player moves the cursor to the top of the screen the character will look "North"; if the player puts the cursor in the bottom left corner, the character will look "Southwest", if that makes sense.

I tried using transform.LookAt, but this caused a crazy feedback loop that made the character spin out of control:

   Ray mouseRay = playerCamera.ScreenPointToRay(Input.mousePosition);
   float midPoint = (transform.position - playerCamera.transform.position).magnitude * 0.5f;
   transform.LookAt(mouseRay.origin + mouseRay.direction * midPoint);

I also tried the Brackey's tutorial here, but was unable to calculate the lookDir because ScreenToWorldPoint returns a Vector2, while a 3D object's transform.position is a Vector3.

Any ideas?

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

2 Replies

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

Answer by Acegikmo · Apr 28, 2020 at 08:37 PM

I'd raycast against a mathematical plane at the height of the player, then use look rotation!

 Ray mouseRay = playerCamera.ScreenPointToRay(Input.mousePosition);
 Plane p = new Plane( Vector3.up, player.position );
 if( p.Raycast( mouseRay, out float hitDist) ){
     Vector3 hitPoint = mouseRay.GetPoint( hitDist );
     player.LookAt( hitPoint );
 }

Comment
Add comment · Show 2 · 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 umbralogian · Apr 28, 2020 at 10:41 PM 0
Share

Thank you for your answer, @Acegikmo! I've accepted this one because of your idea to bounce the Raycast off of a plane at the height of the player character ins$$anonymous$$d of whatever the cursor happens to be over. $$anonymous$$y testing found that this prevents the character rotation from "hanging" on bumps in the terrain or obstacles placed upon it!

avatar image abssuper20 · Dec 15, 2020 at 09:55 AM 0
Share

Your answer is exactly what i was looking for @Acegikmo. But the rotation gets messed up when the mouse is exactly over the player. it keeps rotating around itself like crazy. How do i solve that?

avatar image
1

Answer by Cassos · Apr 28, 2020 at 08:44 PM

So sounds like you just need help with the player looking at the mouse. This should work out:

     void Update()
     {
         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit))
         {
             transform.LookAt(hit.point); // Look at the point
             transform.rotation = Quaternion.Euler(new Vector3(0, transform.rotation.eulerAngles.y, 0)); // Clamp the x and z rotation
         }
     }

It will the player rotate on the y-axis only. To be honest, I'd add a Vector3.Lerp just to make it more smooth! ^^

Greetings,

Max

Comment
Add comment · Show 3 · 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 umbralogian · Apr 28, 2020 at 10:34 PM 0
Share

Hi @Cassos, thank you for your answer! I'm not familiar with Vector3.Lerp; how would it fit into this?

avatar image Cassos umbralogian · Apr 29, 2020 at 10:31 AM 0
Share

It basicly just smooth out the rotation so it doesn't instantly turn to it but needs some time

avatar image ShahinBhai · Feb 22 at 06:09 PM 0
Share

It's worked for me.. Thank you

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

204 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

Related Questions

Is there a way to reset the transform on an object to make it default to those settings?,Is there a way to reset the default transform for an object? 1 Answer

3D top down shooter mouse follow inaccurate. 0 Answers

LookAt() with X axis 1 Answer

Rotate with joystick - LookAt axis flipping 1 Answer

How to make player's projectiles move to the mouse click position in a 3D top down shooter game? 0 Answers


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