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 antwonline · Apr 30, 2013 at 11:37 AM · gameobjectmousepositionrelative

How do I reset the mousePosition relative to an object I click on?

Hi there, As the title suggests, I would like to rotate a platform (that balls bounce off). At the moment it rotates, but is uncomfortable as the platform rotation is equal to the Input.mousePosition.x.

eg. When I would like the move the platform 20degrees, the mouse.x will be at 175, and I need to make is 20 so that it rotates correctly.

Here is the code.

 var lastMousePosition : Vector3;
 private var shouldRotate:boolean;
 
 function Start(){
     shouldRotate=false;
  }
  
 function Update() {
     Debug.Log(Input.mousePosition);
 }
  
 function FixedUpdate(){
         
     if(shouldRotate){ //If shouldRotate = true...
      if(Input.GetMouseButton(0)){ //and if MouseButton is clicked
         if(Input.GetMouseButtonDown(0)){ //and held down
             //reset
             lastMousePosition = Input.mousePosition;    
         }
         else {
               transform.root.transform.Rotate(0,0,Input.mousePosition.x - lastMousePosition.x);
         }
             lastMousePosition = Input.mousePosition;
     }
     }
      
 }
 
 function OnMouseUp(){
  shouldRotate=false;
 }
 
 function OnMouseDown(){
  shouldRotate=true;
 }
 
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

Answer by robertbu · Apr 30, 2013 at 01:31 PM

Your question is a bit unclear. You are using the mouse x start position minus the end position for your rotation, so I'm going to assume that if you have a movement of 175 screen units of the mouse you want a rotation of 20 degrees:

 if (Input.GetMouseButton(0) {
     transform.root.transform.Rotate(0,0,(Input.mousePosition.x - lastMousePosition.x)*.1143);
     lastMousePosition = Input.mousePosition;
 }

A couple of observations. Your comments are backwards. GetMouseButton() returns true for every frame the mouse is held down. GetMouseButtonDown() only returns true for the frame the mouse buttons is pressed down. The GetMouseButtonDown() clause is not doing anything since you also set 'lastMousePosition' on line 23. Your code does not need to be in FixedUpdate() and instead should be in Update(). FixedUpdate() is typically used with Ridigbodies.

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

12 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

Related Questions

Is it possible to position a GameObject as relative to a GUI? 2 Answers

How do I move only one selected object, rather than all objects with that script? 3 Answers

Rotate gameobject to face mouse click over time 1 Answer

Moving Objects to mouse position! 1 Answer

Is i possible to make an instantiated object appear in that exact position, when using a plane as a collider, using Input.MousePosition? 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