Rotate and transform in local space

I’ve got a problem with a controller i’m trying to code.
The idea is, that the object faces and moves in the direction the joystick is pointing.
It works as long as there is no camera movement/rotation because the whole movement of the controller is oriented at world space. I have no idea how to change that and need help.

This is my code so far.

if (left_horizontal != 0.0 || left_vertical != 0.0) {
var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
transform.rotation = (Quaternion.Lerp (heroTransform.rotation, Quaternion.AngleAxis(90.0 - angle, Vector3.up), Time.time * (force * moveSpeed)));
controller.Move(transform.forward * force * moveSpeed);
}

Well, you have to use the camera’s vectors instead of global ones. Start by replacing

Vector3.up

with

Camera.main.transform.TransformDirection (Vector3.forward);

You might want to try TransformDirection with Vector3.up or Vector3.right instead, depending on which way your camera was originally oriented in.

I’m fuzzy on the details of what you want, but here is an educated guess:

if (left_horizontal != 0.0 || left_vertical != 0.0) {
    var angle = Mathf.Atan2(-left_vertical, left_horizontal) * Mathf.Rad2Deg;
    var fwd = Camera.main.transform.forward;
    fwd.y = 0.0;
    var q = Quaternion.FromToRotation(transform.forward, fwd) * transform.rotation;
    var q = Quaternion.AngleAxis(angle, Vector3.up) * q;
    transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * rotationSpeed);
conroller.Move(transform.forward * force * moveSpeed);
}

If your current code is working correctly, there are still aspects that don’t make sense or are wrong. For example, I don’t know what ‘heroTransform.rotation’ is, and the use of Time.time in the Lerp() is wrong.

‘rotationSpeed’ is float you need to define and assign. Start with a value of 5.0.