-1

I'm pretty new to unity and i use it in a special way of creating a non-game2D application So i work only with UI element. I'm having some issues with something i want to do . I have an object which rotate following the finger (or mouse) but the object must not get out of the "area" Here is my code for now :

using UnityEngine;
using System.Collections;
public class SteeringWheel : MonoBehaviour
{
    public float angleMax = 90.0f;
    private float baseAngle = 0.0f;
    void OnMouseDown()
    {
         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
         pos = Input.mousePosition - pos;
         baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
         baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
     }

     void OnMouseDrag()
     {
         Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
         pos = Input.mousePosition - pos;
         float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
         Debug.Log("ang1 : " + ang);
         ang = Mathf.Clamp(ang, -90, 90);
         if (transform.eulerAngles.z >= 89 && transform.eulerAngles.z <= 271)
         {
             transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, 0.7f, transform.rotation.w);
             return;
         }       
         transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);        
     }
 }

And here is a GIF of the problem :

http://img15.hostingpics.net/pics/324271Animation.gif

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • you are accidentally using "Quaternions". Don't use "quaternions" for any reason in Unity. simply set the eulerAngles which is trivial. Or, even easier, just use "Rotate" ("RotateAround" is also very handy.) Cordialement, – Fattie May 18 '16 at 17:23
  • Quaternions are really useful for doing 3D rotations so there's nothing wrong with using them. As for the rotation issues I don't really get why you create a new Quaternion with a fixed Z value. You shouldn't ever create your own quaternion with your own X,Y,Z and W values unless you know how to use them correctly since they're not Euler values. Try exchanging that with an Angleaxis as you already did but with transform.rotation.eulerangles instead – Patrick Dahlin May 18 '16 at 17:41
  • @PatrickDahlin i did this because if i don't, when me cursor goes further than 90° (on the left), the "slider" goes on the right side with an angle of 270° – Dane Girard May 19 '16 at 09:22

2 Answers2

0

Remove the if statement and everything in it, and put the Mathf.Clamp call before the subtraction with baseAngle rather than afterwards.

  • The if statement is faulty because it checks the transform rotation before it is set. It doesn't matter what the previous rotation was, if you're just going to be changing it anyways.
  • The base angle is calculated at runtime, so it does not make sense to clamp it to a predefined range. And anyways, the clamping range seems more appropriate for the value before the baseAngle subtraction.

I don't see any problems with your code that those two changes won't fix.

(I'm assuming, by the way, that baseAngle is in the center of the wheel. If not, you should change your range accordingly.)

Cheers!

HalpPlz
  • 691
  • 4
  • 11
0

Thanks for your answers :) I ask my question on Unity Answer and someone answer me I fix my problem like this :

public class SteeringWheel : MonoBehaviour
{
    public float angleMax = 90.0f;
    private float baseAngle = 0.0f;

    void OnMouseDown()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        pos = Input.mousePosition - pos;
        baseAngle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg;
        baseAngle -= Mathf.Atan2(transform.right.y, transform.right.x) * Mathf.Rad2Deg;
    }

    void OnMouseDrag()
    {
        Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
        pos = Input.mousePosition - pos;
        //float ang = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg - baseAngle;
        float ang = Mathf.Atan2(Mathf.Max(pos.y, 0), pos.x) * Mathf.Rad2Deg - baseAngle;
        Debug.Log("ang1 : " + ang);
        ang = Mathf.Clamp(ang, -90, 90);           
        transform.rotation = Quaternion.AngleAxis(ang, Vector3.forward);
    }
}