Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do I make sure character appears to move at same speed vertically and horizontally?

Discussion in 'Scripting' started by mrCharli3, Jul 13, 2023.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    I am making an isometric game in the style of Hades, I am struggling to get my character to move in "screen space", meaning I want it to appear to move at same speed vertically and horizontally. Atm vertically appears much slower.


    Code (CSharp):
    1. private void Update()
    2.     {
    3.         float horizontalInput = 0;
    4.         float verticalInput = 0;
    5.  
    6.         if (Input.GetKey(KeyCode.W)) verticalInput = 1;
    7.         if (Input.GetKey(KeyCode.S)) verticalInput = -1;
    8.         if (Input.GetKey(KeyCode.D)) horizontalInput = 1;
    9.         if (Input.GetKey(KeyCode.A)) horizontalInput = -1;
    10.  
    11.         // Get the camera's forward and right vectors in screen space
    12.         Vector3 cameraForward = mainCamera.transform.forward;
    13.         Vector3 cameraRight = mainCamera.transform.right;
    14.  
    15.         cameraForward.y = 0f; // Ignore vertical component
    16.         cameraRight.y = 0f; // Ignore horizontal component
    17.  
    18.         // Normalize the camera vectors
    19.         cameraForward.Normalize();
    20.         cameraRight.Normalize();
    21.  
    22.         Vector3 move = (cameraForward * verticalInput) + (cameraRight * horizontalInput);
    23.         move.Normalize();
    24.  
    25.         characterController.Move(move * Time.deltaTime * speed);
    26.     }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Sounds like an odd thing to do but if you need this then scale the vertical speed appropriately. Calculate the horizontal/vertical aspect and use that as the scale for vertical movement.

    If your horz/vert aspect is 1.25 then that's the vertical movement scale.

    Essentially you're saying that due to projection, the apparent speed looks slower even though it's the correct speed.
     
    Yoreki likes this.
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    First of all, you could use input axis to make the whole input part a lot more convenient to work with:
    Code (CSharp):
    1. float h = Input.GetAxis("Horizontal");
    2. float v = Input.GetAxis("Vertical");
    You can also turn it into an input vector and normalize that to get a consistant vector length, independant of the inputs. Right now you can independantly move at 1 vertically and 1 horizontally, which may or may not be what you want. Edit: Well you normalized the move vector afterwards tho
    Code (CSharp):
    1. Vector2 input = new Vector2(h, v).normalized();
    With that out of the way, i dont think it's a good idea to base your movement directons on the camera perspective. Is there any reason the game should not play the same, independant of how you look at it? I would simply base the movement direction on the forward and right of the object itself. You can then, if you need to, scale the movement accordingly depending on the camera perspective, to adjust for how the movement speed "feels".
     
    Last edited: Jul 13, 2023
    MelvMay likes this.
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Not sure why its odd, its how Hades does it.

    I tried scaling it with something like this, but gave me same "issue" :


    Code (CSharp):
    1.  float cameraAngle = Vector3.Angle(mainCamera.transform.forward, Vector3.up);
    2. movementScale = Mathf.Cos(cameraAngle * Mathf.Deg2Rad);
     
  5. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    956
    Its exactly what I want, I do not want the acceleration that comes with using the axis directly.
     
  6. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,588
    mrCharli3 likes this.
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    That's not exactly what I suggested though so. I can see you're trying to change it based upon the camera angle but are you saying the angle is changing? The correct combination Cos/Sin/angle in theory will give you the correct scaling but if it's "unchanged" it simply means you did it wrong. I didn't debug the code you posted in my head so I'd say debug it. If you've done that and you're saying you scaled the movement by a value you think is correct then you applied it wrong. Either way, you made a mistake.

    Just add a scaling explicitly yourself to see it won't be "unchanged" as it's a simple scaling based upon the pespective.

    And to be clear, it is "odd" in the sense that it's incorrect movement in the game world; I didn't say it's objectively wrong. To have a movement in screenspace in a isometric game is "odd" or maybe the term "unusual" better.

    Not everyone knows a game by its name btw! ;)