Home Game Development c# – Implement grid-based motion in a Unity 2D isometric recreation

c# – Implement grid-based motion in a Unity 2D isometric recreation

0
c# – Implement grid-based motion in a Unity 2D isometric recreation

[ad_1]

I’m making a 2D turn-based isometric technique recreation in Unity.

In this recreation, the seen map is split into logical tiles. A personality occupies a single tile at a time, and a tile can host as much as one character at a time. The participant can navigate their character by this grid utilizing a D-pad (Up, Down, Left Right). A personality can transfer from tile-A to tile-B if:

  • Tile-B doesn’t host a personality
  • Tile-B is “traversable”
  • |tile-A.top - tile-b.top| <= 1

I adopted this tutorial to arrange my isometric recreation atmosphere. I used to be in a position to get a primary motion script working, however this merely strikes the sprite throughout the display, it does not bind it to the logical grid.

I’m not asking for somebody to simply do the work for me, however can somebody level me in the proper path? I’ve tried googling and have not gotten helpful outcomes.

Here is my present motion script. It clearly does not implement grid logic, it simply instantly manipulates display place. I simply do not know the place to start out for switching to the motion schema described above.

utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;

[RequireComponent(typeof(SpriteRenderer))]
public class Simple_Char_Move : MonoBehaviour
{
    [SerializeField]
    non-public float velocity = 1;
    
    non-public SpriteRenderer characterSprite;

    // Start is named earlier than the primary body replace
    void Start()
    {
        characterSprite = GetComponent<SpriteRenderer>();
    }

    // Update is named as soon as per body
    void Update()
    {
        MoveCharacter();

        FlipSpriteToMovement();
    }

    void MoveCharacter()
    {
        //I'm placing these placeholder variables right here, to make the logic behind the code simpler to know
        //we differentiate the motion velocity between horizontal(x) and vertical(y) motion, since isometric makes use of "faux perspective"
        float horizontalMovement = Input.GetAxisRaw("Horizontal") * velocity * Time.deltaTime;
        //since we're utilizing this with isometric visuals, the vertical motion must be slower
        //for some purpose, 50% feels too gradual, so we shall be going with 75%
        float verticalMovement = Input.GetAxisRaw("Vertical") * velocity * 0.5f * Time.deltaTime;


        this.remodel.Translate(horizontalMovement, verticalMovement, 0);
    }

    //if the participant strikes left, flip the sprite, if he strikes proper, flip it again, keep if no enter is made
    void FlipSpriteToMovement()
    {
        if(characterSprite != null )
        {
            if (Input.GetAxisRaw("Horizontal") < 0)
                characterSprite.flipX = true;
            else if (Input.GetAxisRaw("Horizontal") > 0)
                characterSprite.flipX = false;
        }
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here