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 15, 2012 at 06:39 PM · rotationmouseangledrag

How to calculate angles and apply to rotate an object.

I have an object that is a circle. I want to be able to rotate the circle around it's center point which will always stay the same (currently I have it at 0,0,0).

I can get two other points in space, basically a start point (x1,y1) and then an end point (x2,y2) based off of where the mouse is clicked and then where the mouse is dragged to.

From this I can get the distance from the the center to the start and end point and from the stop to the end point to get a triangle. The problem from there is I'm not sure how I can calculate the angles.

If I am thinking this through correctly I need to get the angles of the start point and end point and then use those to derive the angle of the center point. Then somehow set the rotation of the object to the angle of the center point.

I have a little diagram below. I'm very dusty on my trig and still learning the Unity language so any help would be much appreciated!

Here is the code I have so far, I'm stuck at how to get all three angles and then what I should use to apply the angle to a rotation once I find it.

 using UnityEngine;
 
 public class SpinWithMouse : MonoBehaviour
 {
     public float speed = 5f;
 
     Transform mTrans;
     Vector3 currentLoc;
     Vector3 startTouch;
 
     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
         startTouch = UICamera.currentTouch.pos;
         
         //Get the distance of x,y,z from the center
         currentLoc = startTouch - transform.localPosition;        
     }
 
     void OnDrag (Vector2 delta)
     {
         //Set the x,y,z of where the mouse is currently at
         Vector3 dragTouch = UICamera.currentTouch.pos;
         
         //Get the distance of x,y,z from the center
         Vector3 newLoc = dragTouch - transform.localPosition;
         
         Debug.Log("Wheel location " + mTrans.localPosition);
         Debug.Log("Clicked location " + currentLoc);
         Debug.Log("Dragged location " + newLoc);
         Debug.Log("Start Touch " + startTouch);
         Debug.Log("Drag Touch " + dragTouch);
     }
 }

alt text

rotate.png (11.1 kB)
Comment
Add comment · Show 8
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 08:09 PM 0
Share

Stil at a loss, would really appreciate any guidance even a push in the right direction.

avatar image Fattie · Oct 15, 2012 at 08:15 PM 0
Share

unitygems.com

avatar image Dakcenturi · Oct 16, 2012 at 01:56 AM 0
Share

Read through most of that. Problem is I can't really use Quaternion.LookRotation because the location I would be looking at is a Vector2 not a Quaternion. Unless I'm missing something?

I can't imagine this is too difficult for someone familiar with how this works, but as I'm still learning I'm having a hard time wrapping my head around it.

avatar image Fattie · Oct 16, 2012 at 05:51 AM 0
Share

TBC ... all you want to know is how to calculate the angle shown between the two lines

AB and AC

is that correct?

spark has given you the answer! just use the unity function "Angle"

Unity has amazingly helpful functions. often if you simply search on the WORD involved (eg "angle") you'll find it !

so it's Vector2.angle( B-A, C-A );

you shoudl TIC$$anonymous$$ his answer if it helps to keep the board tidy, cheers!

avatar image Dakcenturi · Oct 16, 2012 at 09:02 AM 0
Share

So I played with the LookRotation a little bit more and I almost have it working. There is one problem, it turns the whole object to face the mouse rather than just turning it around the z-axis toward the mouse.

I tried limiting the x and y as per the example in unitygems, but then it just doesn't rotate at all.

Here is where I am at, if the x,y rotation can be stopped somehow and only the z rotation this would work perfectly.

using UnityEngine;

public class SpinWith$$anonymous$$ouse : $$anonymous$$onoBehaviour { public Transform target; public float speed = 1f;

 Transform mTrans;
 Vector3 currentLoc;

Vector3 startTouch; 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; startTouch = currentLoc - mTrans.localPosition; }

 void OnDrag (Vector2 delta)
 {
     //Set the x,y,z of where the mouse is currently at
        Vector3 newLoc = UICamera.currentTouch.pos;

Vector3 currentTouch = newLoc - mTrans.localPosition;

     //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.LookRotation(newLoc - currentLoc);

        Debug.Log("Position " + UICamera.currentTouch.pos);
        Debug.Log("Wheel location " + mTrans.localRotation);
 }

}

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by sparkzbarca · Oct 16, 2012 at 02:12 AM

this is a 2d rotation?

then you want to rotate around the y axis angle degrees

angle can be found using the angle function

angle = vector2.angle(from angle(or first click pos), to angle(second click)

rotate(vector3.up, angle)

if good mark as answered if you need more help comment.

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

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

Getting the angle between two screen points based on them not screen orgin 1 Answer

Angle of parametric path 1 Answer

Get angle from mouse direction? 1 Answer

3D Turret Rotation 0 Answers

LookTo Rotation Mouse Drag 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