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 Lachee1 · Jan 20, 2013 at 05:38 AM · 2drotationpositionmousepivot

Rotate plane on a pivot to the mouse position (in 2D)

Hello unity community again!

I have a more rudimentary question now, however, I am struggling to find a solution.

I have a 2D player sprite on a plane, all rigged for sprite animation and movement. However, I would like to add a black coloured plane just behind that will simulate his other hand. This hand is meant to follow the mouse.

However, No matter what script or function i use, his arm never follow the mouse properly and often disort it (as i have it in another object ot get a new pivot.)

Is there any way to fix it?

 //var tmpPos : Vector3 = Camera.main.ScreenToWorldPoint (Vector3 (Input.mousePosition.x,Input.mousePosition.y,0));
 
     //MousePositionWorld = Vector3(-tmpPos.x,-tmpPos.y, transform.position.z);
 
 
 
     //Arm.rotation = Quaternion.FromToRotation(transform.position, -MousePositionWorld);
 
     //Arm.LookAt(MousePositionWorld, Vector3.up);
 
 var mousePos = Input.mousePosition;
 
     // get the mid-point distance between camera and object
 
     mousePos.z = transform.position.z;//Vector3.Distance(Camera.main.transform.position, transform.position)/2;
 
     // look at this world point
 
     Arm.LookAt(Camera.main.ScreenToWorldPoint(mousePos));
 

Here is a picture to help you understand on what exactly I want to do.

![alt text][1]

Thank you for your help in advance, hope it is resolved soon. -Lachee [1]: /storage/temp/6886-answerquestion02.png

answerquestion02.png (11.9 kB)
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

1 Reply

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

Answer by sparkzbarca · Jan 20, 2013 at 07:43 AM

Arm.rotation = Quaternion.FromToRotation(transform.position, -MousePositionWorld);

this isn't right

FromToRotation (FromDirection, ToDirection)

DIRECTION

not position.

your position is the not the same thing as your facing. Your facing can change without having changed your position.

use transform.RotateAround(point, axis,angle)

for this next part

point = point as which the arm connects with the body, the pivot point. You'll have to pick a point

A = whatever direction your starting from or in this case A = point on the plane you want to end up where the mouse is - point

B = where you want to end up or in this case B = mouseposition - point

axis = vector3.cross(A, B);

angle = (i think) mathf.atan2(Magnitude(axis), vector3.dot(A, B));

Best of luck.

Mark as answered.

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

10 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

Related Questions

How to rotate an object by its pivot in script 3 Answers

Mouse position not returning actual position 0 Answers

Change Sprite Origin in 2D 1 Answer

Rotation of GameObject w/ no movement 2 Answers

How to position objects with mouse? 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