- Home /
How to Get 360 degree of angle while dragging object
I tried to find angle of my circle while i drag it around. I can find this if my main Camera is not rotated, but in this case my main Camera is rotated in X axis 90 degree
// This Script is working if the Camera doesn't rotated
void Update () {
if (Input.GetMouseButton(0))
{
Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
transform.position = new Vector3(fingerPos.x, fingerPos.y, transform.position.z);
float angle = Mathf.Atan2(transform.position.y, transform.position.x) * Mathf.Rad2Deg;
if (angle < 0)
angle += 360;
}
}
How do it find angle of the sphere if my Camera.main.transform.Rotate(90,0,0); ?
With 90 degrees of rotation, you would be looking down on the object? Is that the problem? If so, do you want the rotation and angle looking down the Y axis (i.e. on the xz plane)?
Yes i.e on the xz plane and i want the rotation, i have tried this but doesn't work
Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
transform.position = new Vector3(fingerPos.x, transform.position.z, fingerPos.z);
float angle = $$anonymous$$athf.Atan2(transform.position.z, transform.position.x) * $$anonymous$$athf.Rad2Deg;
if (angle < 0)
angle += 360;
Debug.Log(angle);
Answer by prototype7 · Mar 15, 2013 at 12:55 AM
This will do the same too. Transform.InverseTransformPoint: Transforms the position x, y, z from world space to local space. And try to use transTemp to detect your first position before you drag, rotate, and find angle between your firt position and next position.
public Transform transTemp; // Drag gameobject empty in the inspector
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
transTemp.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
if (Input.GetMouseButton(0))
{
Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5f));
transform.position = new Vector3(fingerPos.x, transform.position.y, fingerPos.z);
Vector3 relative = transTemp.InverseTransformPoint(fingerPos);
float angle = Mathf.Atan2(relative.x, relative.z) * Mathf.Rad2Deg;
if (angle < 0)
angle += 360;
Debug.Log(angle);
}
}
Answer by robertbu · Mar 14, 2013 at 04:22 PM
The Z value you are using in your ScreenToWorldPoint() does not match your 'Y' value unless your object happened to be at '5'. I modded you code a bit. Try this:
void Update () {
if (Input.GetMouseButton(0))
{
Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Mathf.Abs(transform.position.y - Camera.main.transform.position.y)));
transform.position = fingerPos;
float angle = Mathf.Atan2(transform.position.z, transform.position.x) * Mathf.Rad2Deg;
if (angle < 0.0f)
angle += 360.0f;
Debug.Log ("angle="+angle);
}
}
Note the angle you are calculating is between your object and the world origin. If you moved the camera (i.e. it was no longer pointing at the origin), then you code would appear to not work.
Hi robertbu Thanks, with this i can find the 360degree of angle while i drag the object, but i have to drag the object far away from original point and rotate it to get the right angle.
I don't have a full understanding of your problem, but I'm going to guess. Your object is not at the origin. As mentioned in the code above, "angle you are calculating is between your object and the world origin." Let's say you have the transform of the object in the middle, say it's called "tr$$anonymous$$iddle." Change the code as follows to calculate the angle if the object is not at the origin.
void Update () {
if (Input.Get$$anonymous$$ouseButton(0))
{
Vector3 fingerPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, $$anonymous$$athf.Abs(transform.position.y - Camera.main.transform.position.y)));
transform.position = fingerPos;
fingerPos = fingerPos - tr$$anonymous$$iddle.position;
float angle = $$anonymous$$athf.Atan2(fingerPos.z, fingerPos.x) * $$anonymous$$athf.Rad2Deg;
if (angle < 0.0f)
angle += 360.0f;
Debug.Log ("angle="+angle);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613094359im_/https://answers.unity.com/themes/thub/images/avi.jpg)