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 Andrey86 · Jun 06, 2013 at 06:02 AM · raycastquaternionslerp

Rotate object towards mouse cursor when click

Hi guys, This is killing me for hours... I want simply to rotate an object towards the mouse cursor when I click. The scripts works, but in its current state when I click the object rotates just a little bit. I have to click 15 times to get the object to rotate in the right position. When I use just GetMouseButton, I can hold the mouse button and the object follow the mouse fine, but I want to rotate the object at the cursor with just one click. Thank you in Advance!

 function Update () {
     if (Input.GetMouseButtonDown(0)) {    
     
     Turn();
 
 }    
 }
 
 function Turn () {
 
     var newPlane = new Plane(Vector3.up, transform.position);
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             var hitDistance = 0.0;
         
             
                 newPlane.Raycast(ray, hitDistance);
                 var targetPoint = ray.GetPoint(hitDistance);
         
 
         var newRot = Quaternion.LookRotation(transform.position - targetPoint, Vector3.up);
             transform.rotation = Quaternion.Slerp(transform.rotation, newRot, Time.deltaTime * 2);
 
 }
Comment
Add comment · Show 6
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 Imankit · Jun 06, 2013 at 08:47 AM 0
Share

Use the Transform.LookAt function in LateUpdate for that..

avatar image Andrey86 · Jun 06, 2013 at 10:42 AM 0
Share

ankit.tiks007 The problem with the Transform.LookAt is that it immediately snaps on the mouse click, and I want the object to smootly turns :)... Or is it there anyway to do that with LookAt?

avatar image dandago · Jun 06, 2013 at 10:43 AM 0
Share

You can probably use something like iTween.RotateTo().

avatar image Andrey86 · Jun 06, 2013 at 10:45 AM 0
Share

Thanks for the answer dandago! I got a hint from another guy that my turning should be updated, because the click is too short time wise. $$anonymous$$aybe I can add a boolean, or loop until a condition is met. I will try that tonight.

avatar image Andrey86 · Jun 06, 2013 at 10:57 AM 0
Share

dandago, I have never heard of iTween.RotateTo() I will check it out in the manual. Thanks man! ;)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dandago · Jun 06, 2013 at 09:05 AM

You can try solving this easily as follows:

  1. Find out where the mouse is in the game world (as you are doing with ScreenPointToRay).

  2. Translate (move) the ray's intersection point so that its y-coordinate matches that of the object you want to rotate. This is to make sure that the object rotates only left/right towards the object, and not e.g. up/down. To get the intersection point, you will need to use a different overload of Physics.Raycast that takes a RaycastHit out parameter.

  3. use Transform.LookAt() to make the object face the point you calculated above.

If this seems a little complicated, skip #2, and then do it later.

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

Raycasting and changing player rotation accoording to what is underneath. 0 Answers

Arrow not shooting the in right orientation 0 Answers

Quaternion Slerp help 1 Answer

move object and slow down near end 1 Answer

Instantiate at hit.point 2 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