Home > database >  how to move pawn to tiles by storing the offset (keep track of current position)
how to move pawn to tiles by storing the offset (keep track of current position)

Time:01-16

I'm trying to recreate the mario party board game. For this, I need my pawn to be able to move to tiles whether they are "on/off" by sending raycasts into them. Now I'm trying to move my pawn a certain amount of distance using DOTWeen. Now I can move my pawn to the left or right from the starting tile, but when I try to move it back to the first tile it will go all the way to the most outer tile. This is my setup screen for now, but I intend to add more tiles, so it won't always have this kind of layout: [1]: https://i.stack.imgur.com/HEwBf.png

I tried to simply add an offset "newPos" and first make it a vector3 of 0, 0, 0 so that it won't influence the first move. And then after every move. Add the new position to that variable. But this doesn't work as intended. How could I use such offset to keep track of where my pawn is located?

Thanks!

code:

public class PlayerMovement : MonoBehaviour
{
    Vector3 startPos;
    Vector3 newPos;
    private void Start()
    {
        print(transform.position);
        startPos = gameObject.transform.localPosition;
        newPos = new Vector3(0, 0, 0);

    }

    void Update()
    {

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            gameObject.transform.DOLocalMove(startPos   newPos   new Vector3Int(-5, 0 , 0), 1);
            
            print(gameObject.transform.localPosition);
            newPos = gameObject.transform.localPosition;
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            gameObject.transform.DOLocalMove(startPos   newPos   new Vector3Int(5, 0, 0), 1);
           
            print(gameObject.transform.localPosition);

            newPos = gameObject.transform.localPosition;
        }
    }
}

Edit: by adding the offset "newPos" it does not work at all. It moves to a whole different location than intended. But this was my attempt to solve the problem. For making it work without the offset simply remove the newPos variable entirely

CodePudding user response:

DOLocalMove moves in local space but it still moves to a target. It does not take the current position into account. When you try to add id by setting newPos you do so at while the sequence is still in progress. Thus creating the erratic behavior. If you want to do it this way, you need to call it after the sequence is completed.

gameObject.transform.DOLocalMove(startPos newPos new Vector3Int(-5, 0, 0), 1).OnComplete( ()=> { newPos = gameObject.transform.localPosition; });

But that leads to the next issue of you having to block input while the sequence is still playing.

It would likely be better to not move your player to fixed positions for game board but instead get the position of the target tile and set its position is the target for your Dotween sequence.

CodePudding user response:

The character always moves left/right relatively to its initial position startPos which is always (0, 0, 0).

For example the following line always makes the character move 5 units to the right from (0, 0, 0).

gameObject.transform.DOLocalMove(startPos   new Vector3Int(5, 0, 0), 1);

What you want is something like this:

public class PlayerMovement : MonoBehaviour
{
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            gameObject.transform.DOLocalMove(gameObject.transform.localPosition   new Vector3Int(-5, 0 , 0), 1);
        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            gameObject.transform.DOLocalMove(gameObject.transform.localPosition   new Vector3Int(5, 0, 0), 1);
        }
    }
}
  • Related