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
0
Question by tonycoculuzzi · Aug 30, 2011 at 09:16 PM · rotationmouseplane

How do I create a shape that rotates from one pivot point, to always point towards the mouse cursor?

I have a plane that is set on a pivot point (model inside a game object set at the pivot point) that I would like to rotate to always face my mouse. How would I go about doing this? I've looked into Transform.LookAt() and it doesn't apply in this situation because the plane needs to look at the mouse in the Y axis.

alt text

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
0

Answer by Waz · Aug 30, 2011 at 10:56 PM

LookAt makes Z face a direction. Simplest solution is to change your plane so that Z is perpendicular, eg. by wrapping it in an empty parent object and rotation the plane relative to the parent.

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
avatar image
0

Answer by aldonaletto · Aug 30, 2011 at 11:34 PM

You must create a plane (a math plane, not the mesh) at the pivot point, and perpendicular to its axis; create a ray from the mouse pointer, and use Plane.Raycast to find the point where the mouse ray hits the plane; use GetPoint to get a 3D point at this position, then finally create a rotation to look at its direction.
I have no enough information to know which axis you're using in the pivot, and I'm not sure if Y is the axis which should look at the mouse pointer. For this reason, I defined these axes in the form "Vector3(x,y,z)", where the axis selected is 1 and the others 0; modify this if you need:

function Update(){ // Define the pivot axis as Vector3(1,0,0) (the X axis)
    var plane = Plane(Vector3(1,0,0), transform.position);
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var distance: float;
    if (plane.Raycast(ray, distance)){
        var direction = ray.GetPoint(distance)-transform.position;
        // create rotation from Y axis to the mouse pointer direction 
        var rot = Quaternion.FromToRotation(Vector3(0,1,0), direction);
        transform.rotation = rot;
    }
}
I haven't tested this; let me know if you have any problem.
Comment
Add comment · Show 1 · 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 tonycoculuzzi · Aug 30, 2011 at 11:39 PM 0
Share

The pivot rotation is in Z, and Y is the axis that will be point to the mouse cursor. I'm using C#, so I'll translate it and post it in C# if it works. Thanks! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

spinning an object with mouse movement 1 Answer

Smooth camera rotation on mobile and pc when GetMouseButton()? 0 Answers

How do I rotate Z axis based off mouse click and drag? 2 Answers

Problem with rotation script. 3 Answers

Keep objects current rotation, with Input Mouse X 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