- Home /
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
(),joystick.Vertical ())*Mathf.Rad2Deg, transform.eulerAngles.z);
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);
}
}
Answer by FlaSh-G · Jul 31, 2017 at 10:09 PM
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 ;)
Thank you for you help.
I tried first variant and it became better. Second variant did not work for me. But i still have same problem that when i drag joystick down my character goes up, same thing for dragging joystick up(he also goes up). Character rotation i perfect, he faces the right direction, but movement is wrong. When i turn off the rotation, he moves right. I think the problem is with movement code. Thank you again, i am going to try fix this :)
There is a invert option in the input settings this might help with the player going in the wrong direction.
Ah my bad, I kinda ignored that part of your question. The good thing is: It's easy to solve.
Have a look at Transform.Translate, more specifically: The second parameter.
Space relativeTo = Space.Self
This means that the movement performed by Translate is relative to the object's direction. So "(0, 0, 1)" means "forward relatively to the object", not "forward along the global z axis". So when your character looks towards (0, 0, 1) and goes towards (0, 0, 1), it's fine. But when it looks towards (0, 0, -1), also going towards (0, 0, -1) means the opposite direction, because "backwards" now points in the other direction for the character.
Long story short - Change your Translate call to this:
var direction = new Vector3(joystick.Horizontal(), 0f, joystick.Vertical());
transform.Translate(direction * moveSpeed * Time.deltaTime, Space.World);
After all this, however, you will sooner or later find that Transform.Translate is a bad choice if your object is supposed to interact with the world around it. Translate ignores colliders around it and if a rigidbody is attached to the object, it's constantly just trying to fix Translate having moved your object into others.
You can go with Translate for now, but keep in $$anonymous$$d that when you run into collision problems, this might be the reason.
Your answer
