# Joystick Character movement.

Hello!
I am trying to make my character move with joystick and want to make him face direction he moving.
But i faced some problems and don’t know how to solve them. I decided to add eulerangles code and character faces right direction but now movement is not working right.

When i move joystick down, character goes up, when i move joystick up character also goes up.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movech : MonoBehaviour {

``````public float moveSpeed;
public VirtualJoystick joystick;
// Use this for initialization
void Start () {
moveSpeed = 15f;
}

// Update is called once per frame
void Update () {
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, Mathf.Atan2(joystick.Horizontal
transform.Translate (moveSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime, 0f,
moveSpeed * Input.GetAxis ("Vertical") * Time.deltaTime);
transform.Translate (moveSpeed * joystick.Horizontal () * Time.deltaTime, 0f, moveSpeed *
joystick.Vertical () * Time.deltaTime);
}
``````

}

I can promise you that using trigonometry is pretty much never needed in Unity. Ever. There’s always a function that allows you to create a cleaner and easier solution, usually in the Quaternion struct or the Vector3 struct.

As far as I can see, you’re going for 3D here. To immediately rotate your character to a specific direction, the simplest way is to just set Transform.forward:

``````var input = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
if(input != Vector3.zero)
{
transform.forward = input;
}
``````

Alternatively, you can create a Quaternion representing this rotation using Quaternion.LookRotation:

``````Quaternion targetRotation;

void Update()
{
var input = new Vector3(joystick.Horizontal, 0, joystick.Vertical);
if(input != Vector3.zero)
{
targetRotation = Quaternion.LookRotation(input);
}
// Rotate towards targetRotation here
}
``````

And then, to have the character steadily rotate towards the set target rotation (as the code comment suggests), use Quaternion.RotateTowards:

``````transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, speed * Time.deltaTime);
``````

This answer might be a bit more than expected already, but basically it’s just to make clear: You don’t need trigonometric functions, just Quaternion and Vector3 functions