Home > Net >  Fixing the movement direction (3d isometric game)
Fixing the movement direction (3d isometric game)

Time:09-16

I am a beginner in game development and I am currently having problem with the movement direction of my character. The game is in a 3D isometric view, and the camera is set to X = 30 and Y = 45 and the projection set to Orthographic. The problem is the forward direction, when the character is moving forward (when i press W) since it is in isometric view, the character is moving 45 degrees northwest or diagonally to the left. It is mainly because the camera is angled for isometric view.

What I want to do is when I move forward, the character will move upward the screen not diagonally. I thought of changing the rotation of the whole assets to change the forward direction but it will take time to do it. I also thought of changing the movement direction through the code, I changed and added a few things, but it just messes up the movement of the player.

Here is the script for the movement of the player (this is the code before the failed trial and error I made):

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{

    public float speed, rotationSpeed;
    public Joystick joystick;

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput);
        movementDirection.Normalize();

        transform.Translate(movementDirection * speed * Time.deltaTime, Space.World);

        if (movementDirection != Vector3.zero)
        {
            Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * Time.deltaTime);
        }
    }
}

CodePudding user response:

You could correct your movementDirection by applying a rotation to it.

Something like:

movementDirection = Quaternion.Euler(0,45f,0) * movementDirection;//rotates by 45° on y axis

before you do your translation and orientation.

  • Related