x


Rotating character based on joystick angle, at an angle

Hi!

I'm trying to control and rotate a player character based on the joystick input like e.g. Torchlight and Magicka.

If you hold the joystick up-left, I want the player character to immediately rotate and face that direction and walk to that position, while having the character always being angled at e.g. a 30 degree angle.

If you hold the joystick down-right, the player character should rotate and face that direction and walk there.

The problem I'm having is keeping the player character at the 30 degree angle while rotating him towards the joystick angle. Here's an image trying to explain what I currently have (the "Wrong" one), and what I hope to achieve (the "Right" one):

alt text

Here's the code I currently use, which works for everything BUT does not keep the player at the right 30 degree angle:

public const float WALK_SPEED = 250.0f;

float axisHorizontal = Input.GetAxis("Horizontal (J)");
float axisVertical = Input.GetAxis("Vertical (J)");

float translateX = (axisHorizontal * WALK_SPEED) * Time.deltaTime;
float translateY = (axisVertical * WALK_SPEED) * Time.deltaTime;

// move the player towards the joystick direction
GoPlayer.transform.Translate(translateX, translateY, 0.0f, Space.World);

// rotate the player towards the joystick direction
GoPlayer.transform.localRotation = Quaternion.Euler(0.0f, (Mathf.Atan2(axisHorizontal, axisVertical) * Mathf.Rad2Deg), 0.0f);

// reset Z value to a constant value, since it's modified by the above code
GoPlayer.transform.position = new Vector3(GoPlayer.transform.position.x, GoPlayer.transform.position.y, 100.0f);

Basically, I want what the line below does, as it rotates it correctly at the angle it's currently on, but instead rotate it to the joystick's direction:

GoPlayer.transform.Rotate(Vector3.up * (Time.deltaTime * 100.0f));

Thanks!

more ▼

asked Jun 15 '11 at 02:51 AM

TMK gravatar image

TMK
60 7 7 13

In Magicka, the character moves on the horizontal plane (ZX), but in this script the character goes up and down (Y axis) instead of forth and back. Is it the intended behavior?

Jun 15 '11 at 04:17 AM aldonaletto

Hi aldonaetto. The way I'm currently doing it having the 3D player object on top of a 2D map (made using SM2), so in that regard, I need it to move it along the X/Y axis while keeping the Z axis constant, so it doesn't move behind the 2D map, if that makes any sense :) But I can change the axis of it all though, e.g. make the 2D map be horizontal instead of vertical, whatever makes it work :) Thanks!

Jun 15 '11 at 12:30 PM TMK

If I had to make a game like that, I would use the map at the ZX plane and the camera in perspective projection, adjusting its position in order to have a somewhat similar view, and never would try to tilt the characters. But if you want to reproduce the Magicka style more closely, maybe you have to use the camera in ortographic mode (set it in the Projection field). You can place the map at ZX or XY plane, but I think ZX plane is easier. I think you can get good results without tilting the characters away from the camera. Are you sure you'll need to do that?

Jun 15 '11 at 01:18 PM aldonaletto

Doh, that is a much easier solution! I was thinking about it the wrong way :) I will try to do this instead as it should work out well. Thanks aldonaletto!

Jun 15 '11 at 01:26 PM TMK
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x720
x322
x286
x285
x214

asked: Jun 15 '11 at 02:51 AM

Seen: 1754 times

Last Updated: Jun 21 '11 at 03:21 PM