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 Dakcenturi · Oct 14, 2012 at 08:57 PM · rotationmousez-axis

How can I rotate around the local z axis based off starting location in relation to where the mouse is dragged?

I am trying to rotate a circle around the it's local z axis, but I need it to rotate toward where the mouse is currently dragged to.

I am using NGUI to handle the capture of the drag event. The problem is over looking at all the different methods to handle rotation the closest thing I can find is Quaternion.FromToRotation. The problem here is that I don't know how to capture the start location as every time the circle moves the start rotation needs to move.

The following code "sort of" works in that I can rotate the circle toward where the mouse is, but I have to make a really large circle with the mouse drag around the outter edges of the object. I know this is because the start location is static to where I clicked on the object. Where it seems that after the initial click this start location needs to be updated to the previous location of where the drag took place?

Also, there is the obvious problem that whenever the drag stops and the click starts again it seems to reset the circle.

I am new to coding in 3D, so I am still trying to wrap my head around vectors, Quaternion , eulers and all that.

 using UnityEngine;
 
 public class SpinWithMouse : MonoBehaviour
 {
     public Transform target;
     public float speed = 1f;
 
     Transform mTrans;
     Vector3 currentLoc;
     public float angle = 0f;
 
     void Start ()
     {
         //Set the transform for the script to the transform of the object the script is attached to
         mTrans = transform;
     }
     
     void OnPress()
     {
         //Set the x,y,z that is currently clicked or touched
         currentLoc = UICamera.currentTouch.pos;
     }
 
     void OnDrag (Vector2 delta)
     {
         //Set the x,y,z of where the mouse is currently at
         Vector3 newLoc = UICamera.currentTouch.pos;
 
         //Get the difference between the start angle and the stop angle
         //Quaternion targetRotation = Quaternion.LookRotation(newLoc - currentLoc);
         
         //Set the rotation of the object based on where the click occured and where the mouse is dragged i
         mTrans.localRotation =  Quaternion.FromToRotation(currentLoc,newLoc); //Quaternion.Euler(0f, 0f, -0.5f * delta.x * speed) * mTrans.localRotation;
         
         Debug.Log("Position " + UICamera.currentTouch.pos);
         Debug.Log("Wheel location " + mTrans.localRotation);
     }
 }


alt text

rotate.png (8.9 kB)
Comment
Add comment · Show 2
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 Dakcenturi · Oct 15, 2012 at 12:42 AM 0
Share

Added a drawing of what I am trying to accomplish.

avatar image Dakcenturi · Oct 15, 2012 at 02:20 PM 0
Share

Correct me if I am wrong, but it seems like what I need to find is the angle or A, where as now I am actually comparing the angle of B to the angle of C? If I find the angle of A then I could essentially assign that to the Z of the Euler and get it to rotate correctly?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sparkzbarca · Oct 16, 2012 at 03:08 AM

oh wow thats only a 2d rotation around the Z axis super easy

you want to rotate by the change in the mouse position

the mouse position can be gotten by using

input.mouseposition

 an angle needs 2 vectors to find an angle between
 
 the first vector is the mouse position last frame
 the second is the one this frame
 
 input.mouseposition gives us the position of the mouse right now
 
 input.getaxis("Mouse Y")
 input.getaxis("Mouse X")
 
 gives us a number showing how much change there was between this frame and the last.
 basically its position last frame was its current position minus that change.
 
 so 
 input.mouseposition is a vector2
 
 old_pos = input.mousepostion - new vector2(input.getaxis("Mouse X"), input.getaxis("Mouse Y");
 
 now we have the old position and the new is just input.mouseposition. now we need the angle between them. but there's a function for that
 
 vector2.angle
 
 vector2.angle(from, to)
 
 vecto2.angle(oldpos,newposition)
 
 now we rotate around the z axis angle degrees
 
 wheel.transform.rotate(axis, angle to rotate)
 
 wheel.transform.rotate(vector3.forward, angle)
 
 
 all good?
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 Dakcenturi · Oct 16, 2012 at 08:26 AM 0
Share

This still doesn't get me where I need. All this seems to do is rotate the wheel in counter clockwise direction while I click and drag the mouse.

However, I need it to rotate in either clockwise or counterclockwise direction toward where the mouse is currently located. Almost something like the Quaternion.LookRotation but I haven't been able to get that to work.

Here is the code I am using currently where it just rotates as I move the mouse:

using UnityEngine;

public class SpinWith$$anonymous$$ouse : $$anonymous$$onoBehaviour { public float speed = 5f;

 Transform mTrans;
 Vector3 currentLoc;

 void Start ()
 {
     //Set the transform for the script to the transform of the object the script is attached to

mTrans = transform; }

 void OnPress()
 {

//Get the distance of x,y,z from the center currentLoc = Input.mousePosition - new Vector3(Input.GetAxis("$$anonymous$$ouse X"), Input.GetAxis("$$anonymous$$ouse Y")); }

 void OnDrag (Vector2 delta)
 {
     //Set the x,y,z of where the mouse is currently at

Vector3 newLoc = Input.mousePosition; //Get the distance of x,y,z from the center float angle = Vector3.Angle(currentLoc,newLoc); mTrans.transform.Rotate(Vector3.forward, angle); Debug.Log("Wheel location " + mTrans.localPosition); Debug.Log("Clicked location " + currentLoc); Debug.Log("Dragged location " + newLoc); } }

avatar image
0

Answer by sparkzbarca · Oct 14, 2012 at 09:10 PM

your not trying to rotate around the local Z axis I dont think.

I think your trying to rotate around the axis parallel to the mouse

like this alt text

Am I correct in thinking thats what you want. For the object to spin in place so that it spins from the top of the sphere towards you and then rotates back down?


helping.png (6.1 kB)
Comment
Add comment · Show 3 · 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 sparkzbarca · Oct 14, 2012 at 09:26 PM 0
Share

if thats what you need let me know and i can post some help in doing it just want to make sure I got the problem right first.

avatar image Dakcenturi · Oct 14, 2012 at 09:50 PM 0
Share

I am basically trying to turn the wheel around in a circle, where it rotate around the z-axis.

Here is what I am actually working on: http://www.thegeekenders.com/resources/misc/Zpoc/WebPlayer.html

avatar image Pritamde12 · Aug 07, 2013 at 06:13 AM 0
Share

Have you solved the problem? If yes, could you post the code. Thank you.

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

11 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

Related Questions

How to increase the range of the player aiming? 2 Answers

controller 2 Answers

Authoritative Rotation 2 Answers

Mouse Look from scratch rotating Z axis 2 Answers

Rotation of Object on single axis in direction of the mouse position 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