- Home /
Object rotation using Mathf.Atan2
Gentlemen, I need help from who's more competent, and I apologize for my low level in programming.
I want to rotate on Z axis an object using Atan2 (there are several hints herein) facing the mouse cursor.
The Object is surely rotating but it is always shifted by -90deg compared to the position of the mouse cursor.
If I would like to clamp the rotation between 2 angles (-135 and +135) the rotation stops or jumps to the other limits.
I really appreciate somebody will give me clarifications on my mistakes.
....................... This is the image of the object .
Here is the code:
public class LookAtMouse : MonoBehaviour {
public GameObject TrimKnob;
public float rotation_z;
public float maxAngle;
public float minAngle;
public Vector3 mouse_Pos;
public Vector3 difference;
public Vector3 diffNorm;
public int layerMask ;
void Start() {
maxAngle = 135.0f;
minAngle = -135.0f;
rotation_z = 0.0f;
TrimKnob.transform.Rotate (0f,0f,0f);
}
void Update ()
{
if (Input.GetMouseButton (0)) {
layerMask = 1 << 8;
//OnMouseDown ();
if ((rotation_z > 135f) || (rotation_z < -135f)) {
if (rotation_z == 135.0f){
rotation_z = 135.0f;
} else if (rotation_z == -135.0f){
rotation_z = -135.0f;
}
}
} OnMouseDown ();
}
void OnMouseDown() {
//transform.position = Vector2.Lerp(transform.position, Camera.main.ScreenToWorldPoint(Input.mousePosition), moveSpeed);
mouse_Pos = Input.mousePosition;
mouse_Pos = Camera.main.ScreenToWorldPoint(mouse_Pos);
//difference = TrimKnob.transform.position - Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit hit = new RaycastHit ();
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
Debug.Log ("ray is " + ray.origin);
if (Physics.Raycast (ray, out hit, 15f)) {
Transform objectHit = hit.transform;
Debug.Log (hit.collider.name);
difference = mouse_Pos - TrimKnob.transform.position;
//Debug.Log ("As Vector3, difference = " + difference);
//difference.Normalize();
//diffNorm = difference.normalized;
//Debug.Log ("As normalized, difference = " + diffNorm);
rotation_z = Mathf.Atan2 (difference.y, difference.x) * Mathf.Rad2Deg;
if ((rotation_z< 135.0f) || (rotation_z>-135.0f)) {
Std_Rot ();
}
if (rotation_z >135.0f ){
Pos_Rot ();
}
if (rotation_z <-135.0f ) {
Neg_Rot ();
}
}
}
void Neg_Rot () {
rotation_z = -135.0f;
TrimKnob.transform.rotation = Quaternion.Euler (0f, 0f, rotation_z);
}
void Pos_Rot () {
rotation_z = 135.0f;
TrimKnob.transform.rotation = Quaternion.Euler (0f, 0f, rotation_z);
}
void Std_Rot () {
TrimKnob.transform.rotation = Quaternion.Euler (0f, 0f, rotation_z);
//TrimKnob.transform.LookAt (0f,0f,rotation_z);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612181830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
problem after rotate an object 0 Answers
Rotate Center Pivot based Object like a Draw Bridge 2 Answers
problem after rotate an object 1 Answer