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.
void Update()
{
PlayerInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
// Smooth the input direction
smoothedMovementDirection = Vector2.Lerp(smoothedMovementDirection, PlayerInput, smoothingFactor);
// Update last non-zero movement direction only when the player is moving
if (PlayerInput != Vector2.zero)
{
lastNonZeroMovementDirection = smoothedMovementDirection;
// Update facing direction based on last non-zero movement direction
if (lastNonZeroMovementDirection.x < 0)
{
facingLeft = true;
}
else if (lastNonZeroMovementDirection.x > 0)
{
facingLeft = false;
}
}
}
public Vector2 GetLastMovementDirection()
{
return lastNonZeroMovementDirection;
}
void FixedUpdate()
{
Vector2 moveForce = PlayerInput * moveSpeed;
moveForce += forceToApply;
forceToApply /= forceDamping;
if (Mathf.Abs(forceToApply.x) <= 0.01f && Mathf.Abs(forceToApply.y) <= 0.01f)
{
forceToApply = Vector2.zero;
}