Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Question 2D Isometric Diagonal Movement Angles

Discussion in '2D' started by Dracin, Apr 27, 2024.

  1. Dracin

    Dracin

    Joined:
    Feb 25, 2014
    Posts:
    12
    Hi Everyone,

    I'm very new to writing code but I've managed to piece together a movement script for my 2D isometric game. The problem is, I cant seem to fix the angles when moving in any diagonal direction.

    Currently the player will move along the diagonal direction but never along the lines of the isometric grid I'm using in the tilemap. In the first image we can see the start position of the player on the pink line of tiles. If I move up and to the left he will end up on the green line as he reaches the end (Picture 2). The script is below.

    Any help will be appreciated.

    ESRR unity forum post 1a.JPG

    ESRR unity forum post 1.JPG


    Code (CSharp):
    1. void Update()
    2.     {
    3.         PlayerInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
    4.  
    5.         // Smooth the input direction
    6.         smoothedMovementDirection = Vector2.Lerp(smoothedMovementDirection, PlayerInput, smoothingFactor);
    7.  
    8.         // Update last non-zero movement direction only when the player is moving
    9.         if (PlayerInput != Vector2.zero)
    10.         {
    11.             lastNonZeroMovementDirection = smoothedMovementDirection;
    12.  
    13.             // Update facing direction based on last non-zero movement direction
    14.             if (lastNonZeroMovementDirection.x < 0)
    15.             {
    16.                 facingLeft = true;
    17.             }
    18.             else if (lastNonZeroMovementDirection.x > 0)
    19.             {
    20.                 facingLeft = false;
    21.             }
    22.         }
    23.     }
    24.  
    25.     public Vector2 GetLastMovementDirection()
    26.     {
    27.         return lastNonZeroMovementDirection;
    28.     }
    29.  
    30.     void FixedUpdate()
    31.     {
    32.         Vector2 moveForce = PlayerInput * moveSpeed;
    33.         moveForce += forceToApply;
    34.         forceToApply /= forceDamping;
    35.  
    36.         if (Mathf.Abs(forceToApply.x) <= 0.01f && Mathf.Abs(forceToApply.y) <= 0.01f)
    37.         {
    38.             forceToApply = Vector2.zero;
    39.         }
    40.  
     
    Last edited: Apr 28, 2024
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,519
    Hey could you edit your post to use code tags? Also it's good question-asking practice to post the simplest code that still has the problem so we don't see all the details that aren't relevant to the core problem.

    As for the problem (if I understand it right), if you want to force more isometric-like movement then you want to scale the y-component of the movement down. This is to move slower up-and-down since the character is traversing tilemap cells too quickly by default. For "true" isometric, the y-scalar is .57735 but this looks like traditional isometric and I don't have that off the top of my head. You then normalize the vector2 again then multiply that by your speed. Adapt for your circumstance.

    tldr: you probably want to move slower in the y-direction
     
    Dracin likes this.
  3. Dracin

    Dracin

    Joined:
    Feb 25, 2014
    Posts:
    12
    Hey Lo-renzo, I've edited the post. After some tinkering with the script I finally got my diagonal movement to work! Thank you for the help.
     
    Last edited: Apr 28, 2024