Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by bwaite87 · Jul 17, 2018 at 01:27 AM · rotationmovementik

Top down Shooter upper body look at mouse pos

So I have a top-down shooter type control scheme going on, and currently I have my character looking at the mouse position which works great.

What I'm trying to accomplish is to rotate his hips to look at the mouse position if the angle from the direction he's facing and the mouse cursor is less than 45. This way his feet won't move, his upper body will look towards the mouse cursor, and it'll look more natural.

The below works, but I'm having a few issues with it: First, I think my angle calculation isn't working quite right, if the mouse cursor is directly in front of him it's at about 7-8 degrees as opposed to the 0 I expected.

Also, I'm able to put the mouse cursor slightly behind him, resulting in my character kinda looking like an ostrich trying to eat its own tail...or something lol.

Here's my code, any suggestions?

 public Transform spineBone;
     Vector3 mousePos;
     
     private void LateUpdate()
     {
         if (Vector3.Angle(transform.forward, mousePos) < 45)
         {
             spineBone.LookAt(mousePos);
         }
 
         else
         {
            //rotate the whole character's body
         }
         
     }
 
     void Turning()
     {
         Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit floorHit;
         if (Physics.Raycast(cameraRay, out floorHit, cameraRayLength, floorMask))
         {
             Vector3 playerLookAtMouse = floorHit.point - transform.position;
             playerLookAtMouse.y = 0f;
             mousePos = floorHit.point;
            
 
             Quaternion newLookRotation = Quaternion.LookRotation(playerLookAtMouse); 
             //rb.MoveRotation(newLookRotation);
         }
     }
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 Shameness · Jul 17, 2018 at 02:17 AM 0
Share

if (Vector3.Angle(transform.forward, mousePos) < 45) { mousePos.z = spineBone.z; // or z value of the object that has to rotate. or make both zero spineBone.LookAt(mousePos); }

edit: didn't tried, see if it helps

avatar image bwaite87 Shameness · Jul 17, 2018 at 11:39 PM 0
Share

hmm, seemed to make the problem worse. He's pretty much bent in half all the time now.

Any other suggestions?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bwaite87 · Jul 18, 2018 at 01:36 AM

So after much digging and debugging, I realized I wasn't calculating my angle correctly. I wanted to calculate the angle from my forward facing position to an object.

At the same time, managed to figure out how to limit the lookAt to rotate only on the Y axis. Below is what worked for me:

  public Transform spineBone;
     Vector3 mousePos;
     
     
     private void LateUpdate()
     {
         
         //Debug.Log(Vector3.Angle(transform.forward, mousePos - transform.position));
 
         if (Vector3.Angle(transform.forward, mousePos - transform.position) < 45) //subtracting the transform position from the mousePos got the angle calculation I was looking for
         {
             Vector3 targetPos = new Vector3(mousePos.x, spineBone.transform.position.y, mousePos.z); //this limits the LookAt to only rotate on the X axis by basically saying keep the y axis between the spineBone and the mousePos the same
             spineBone.LookAt(targetPos);
             
         }
 
         else
         {
            //rotate the whole character's body
         }
         
     }

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.

Follow this Question

Answers Answers and Comments

159 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

Related Questions

How to make an object rotate to raycast hit from mouse? Unity 3D 1 Answer

made a ring collider from sphere colliders and a bar collider, but when animated the ring doesn't stay on bar 0 Answers

Help regarding "Collect Cubes" Like Player Movement and Rotation 1 Answer

Rigidbody.MovePosition doesn't move reliably? 1 Answer

World and Local Axis out of Alignment 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