- Home /
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):
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!
In $$anonymous$$agicka, the character moves on the horizontal plane (ZX), but in this script the character goes up and down (Y axis) ins$$anonymous$$d of forth and back. Is it the intended behavior?
Hi aldonaetto. The way I'm currently doing it having the 3D player object on top of a 2D map (made using S$$anonymous$$2), 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 ins$$anonymous$$d of vertical, whatever makes it work :) Thanks!
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 $$anonymous$$agicka 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?
Doh, that is a much easier solution! I was thinking about it the wrong way :) I will try to do this ins$$anonymous$$d as it should work out well. Thanks aldonaletto!
Your answer
Follow this Question
Related Questions
Move and Rotate on a fixed axis? 1 Answer
Making player face the direction it moves (using angles). 1 Answer
3rd person unit control 0 Answers
How to make the npc face the player. 2 Answers