Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 RPDEV · Dec 31, 2013 at 06:52 PM · mouserotatetowards

Smoothing out rotations

Hello. I have a character that is rotating towards the mouse position. The game is played in 2D, but it's in a 3D world similar to Little Big Planet. I got the character rotating towards the mouse, but I wanted to smooth it out and control the rotation speed so it would rotate towards the direction of the mouse rather than rotate exactly towards the mouse all the time. This is where my main problem happens, the way I decided to smooth things out was by taking the angle difference between the mouse/char and then to divide it by rotateSpeed. Then to add that result onto the current rotation, however because it's in quaternions when the mouse moves to the left of the character (which is when the rotation is at 0.7, 0.7) it will instantly jump to -0.7, -0.7 which forces the character to rotate in the opposite direction until it faces the mouse. Does anyone have any ideas for this?

My code is here:

 public void RotateSmoothly(GameObject character, Camera mainCamera)
 {
     Quaternion result;
     RaycastHit hit = new RaycastHit();
     Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);

     Vector3 characterPosition = character.transform.position;
     Vector3 mousePosition = new Vector3();
     float tempRotation = new float();

     if (Physics.Raycast(ray, out hit))
     {
         mousePosition = hit.point;
     }

     tempRotation = Mathf.Atan2((mousePosition.y - characterPosition.y), (mousePosition.x - characterPosition.x)) * Mathf.Rad2Deg;
     tempRotation -= 90; // corrects alignment
     vectorRotation = new Vector3(-Mathf.Sin(Mathf.Deg2Rad * tempRotation), Mathf.Cos(Mathf.Deg2Rad * tempRotation), 0);

     result = Quaternion.AngleAxis(tempRotation, new Vector3(0, 0, 1.0f));

     // attempts at smoothing
     Quaternion r2 = new Quaternion(result.x / rotateSpeed, result.y / rotateSpeed, result.z / rotateSpeed, result.w / rotateSpeed);
     Quaternion r3 = new Quaternion(character.transform.rotation.x + r2.x, character.transform.rotation.y + r2.y, character.transform.rotation.z + r2.z, character.transform.rotation.w + r2.w);

     Debug.Log(r3);
     character.transform.rotation = r3;
 }
Comment
Add comment · Show 1
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 Gruffy · Dec 31, 2013 at 08:37 PM 0
Share

I think you need to look into Quaternion.Slerp bud. Also pop any of your camera logic updates/methods into the provided

 LateUpdate()
 {
 }

as this will update after all the other Update methods have fired n all that so it will only change the camera every couple of frames or so- making the movement you should get on the camera (from the Quaternion.Slerp function/method I mentioned to you to look at earlier) smoother atleast. Link to Unity Quaternion.Slerp reference below

Cheers bud, hope this starts your journey to completion :) Gruffy

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RPDEV · Dec 31, 2013 at 11:36 PM

Thanks homie, that's what I was looking for. Didn't know a function for quats existed for that.

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

18 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

Related Questions

rotate a hand crank with circular mouse motion? 1 Answer

Look around when holding down mousebutton 1 Answer

rotate object to face mouse cursor x/z top down shooter 0 Answers

How do I get the player to correctly rotate toward the mouse? 1 Answer

make turret point where mouse points 3 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