0
votes

I have a class below that I attach to a object in order to make it rotate around its pivot. I sent the pivot of the sprite via the inspector.

This works exactly how I want it too, BUT the issue I am having is that whenever I touch and drag it, and then touch and drag it again, it snaps to a new position.

What I would like for it to do is, when it is rotated and then rotated again, the sprite stays in its same rotation and not snap to a new position and I would like the angle of the sprite to be reset to 0. The next then is that I want the angle to continually rotate. So if I rotate it in the positive direction, the angle should keep increasing in the positive direction and not change..Such as 0---> 360 ----> 720 -----> etc, etc. And then when the mouse is released, the sprite stays in the same position but the angle is now set back to 0. And then when clicked again to rotate, it rotates from that exact position.

Here is my code thus far which works well for rotating, but I would like to modify it to achieve the above scenario. Any help with this?

public class Steering : MonoBehaviour {

float prevAngle,wheelAngle,wheelNewAngle = 0;
public SpriteRenderer sprite;
void Start () {
}  

void Update () {


}

public float GetAngle(){


    return wheelAngle;



}

void OnMouseDrag(){

    Vector3 mouse_pos = Input.mousePosition;
    Vector3 player_pos = Camera.main.WorldToScreenPoint(this.transform.position);

    mouse_pos.x = mouse_pos.x - player_pos.x;
    mouse_pos.y = mouse_pos.y - player_pos.y;

    wheelNewAngle = Mathf.Atan2 (mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;

    if (Input.mousePosition.x > sprite.bounds.center.x) {

        wheelAngle += wheelNewAngle - prevAngle;

    } else {
        wheelAngle -= wheelNewAngle - prevAngle;

    }



    this.transform.rotation = Quaternion.Euler (new Vector3(0, 0, wheelAngle));

    Debug.Log (wheelAngle);
    prevAngle = wheelNewAngle;
}

void OnMouseUp(){


    prevAngle = wheelNewAngle;
    wheelAngle = 0;


}

}

1
the answer given by TheOsirian was actually perfectly correct :)Fattie
Unfortunately this question is just too unclear.Fattie

1 Answers

0
votes

By angle of the sprite, do you mean the rotation? I'm not sure how the position is changing if there's nothing in your code doing that. Does it always move to the same position? I'm having a little trouble visualizing how your system is supposed to look but I hope this helps.

It looks like you might want to store the previous mouse position so you can get the relative amount to rotate each frame.

At the top:

Vector3 prevMousePos = Vector3.zero;

This method will help get the position when the player pressed:

void OnMouseDown(){
    prevMousePos = Input.mousePosition;
}

Then in OnMouseDrag() get the difference between the two mouse positions to get the relative position (if you moved the mouse left, right, up, or down since pressing):

Vector3 mouseDiff = Input.mousePosition - prevMousePos;

With this it will use the relative mouse position after pressing instead of the current one, which should smooth things out.