Hello! I am moving a 2D sprite with a touch-screen circle controller.

I know the Angle between the touch and the center of the circle, and I know the distance between the center and the touch (For the speed of the movement, 0.0-1.0).

What I don’t know is how to convert that angle into the new position for the 2D Sprite.

I’ve tried a few different options, but so far nothing is working as I expect. In the image below, I know moveAngle (0 is “forward”, to the right of the screen).

The movement code I envision, in LateUpdate(), is simply transform.position = newPosition, where newPosition is the position moveSpeed away from the current position at moveAngle. (I multiply the moveSpeed by another number to get the desired actual speed of movement).

This is not about programming, this is about vectors xD. You have to use the angle to create a paralel vector which starts on the character, that would be the vector you use in transform.Translate(Vector3), I don’t recommend you to use transform.position to move objects, it’s not accurate and it can cause graphical issues.

Alright – once I pass the movementAngle (angle of touch vs. center of circle) and the movementValue (distance from center, normalized), this code on the player object works like a charm.

var radians = movementAngle * Mathf.Deg2Rad;
var x = Mathf.Cos(radians);
var y = Mathf.Sin(radians);
var pos = Vector3(x, y, 0); //Vector2 is fine, if you're in 2D
pos = (pos * (movementValue * moveSpeed)) * Time.deltaTime;
var newPos = transform.position + pos;
if (newPos.x < horizontalBounds.x)
newPos.x = horizontalBounds.x;
if (newPos.x > horizontalBounds.y)
newPos.x = horizontalBounds.y;
if (newPos.y < verticalBounds.x)
newPos.y = verticalBounds.x;
if (newPos.y > verticalBounds.y)
newPos.y = verticalBounds.y;
transform.position = newPos;

Sorry if I looked like rude or impolite, it wasn’t my intention.

I mean you can just adapt the vector formula to code, so you can use it to translate the vector created by the pad into your character.

Anyway I think I found another way to do it, you can try to rotate the vector “transform.Translate(Vector3)” using that angle as a Quaternion.Euler, I’ve tested it and it seems to be working. It would be something like this:

void Update () {
float speed = moveSpeed * thenumberyouuse;
transform.Translate (Quaternion.Euler(0, 0, the angle you have) * new Vector3(speed, 0, 0) * Time.deltaTime) //I add "Time.deltaTime" in order to get it framerate independant.
}

Yep, that works as well and is of course much much less code. Thank you!

– Only problem – how would I ensure the player doesn’t move outside of specific positions? I’m not sure how to do that with transform.Translate(), which is why I was figuring out the new position first, and then adjusting it for the play area bounds.

Introduce a verification for position before using the “transform.Translate()”:

void Update () {
if (transform.position.x < maxvalue && transform.position.x > minvalue && transform.position.y < maxvalue && transform.position.y > minvalue){
float speed = moveSpeed * thenumberyouuse;
transform.Translate (Quaternion.Euler(0, 0, the angle you have) * new Vector3(speed, 0, 0) * Time.deltaTime) //I add "Time.deltaTime" in order to get it framerate independant.
} else{
}
}

This should work, you can use both codes, as you prefer. But if you use high speeds you may get a graphical issue in both, the object will go farther than it should and then come back to the maximum position. You can correct that implementing a system which predicts the next position before it moves, so it can be stopped before getting out of the limit.

Have a good day sir.

P.D: The fuck happened with all these spaces in my code? xDD