Movement Issues

Hello,

I am currently working on a RPG type movement system and all things seemed to go well, until I wanted to make my “Map” bigger. My movement is setup to get all “Tiles” I’m colliding with and add to a List, and from that List I created another List of the collided “Tiles” location. If it was on the list, and +0.32/-0.32 depending on direction, move there. The description might be confusing, but it is working in this picture :

The problem is if I copied the tiles in my “Map”, just like I did before and positioned them at the 1.6 mark, either X/Y, they just didn’t work(weren’t picked up by the collision to be added to the list).

I moved my “Map” aka all 5x5 “Tiles”, from 0,0, to -.32,0, and for some reason would then pickup the “Tile” at 0,1.6 and if there was another at 0,1.92 it didn’t register.

Anyone have any thought on this?

how does the code you with which you set up ur lists and you set up your map?

void OnTriggerStay2D(Collider2D col)
    {
        // check we don't already have the collided object in the list
        if(!myCollidedGameObjects.Contains(col.gameObject) && col.tag == "Empty")
        {
            // add it to the list
            myCollidedGameObjects.Add(col.gameObject);
            //myCollidedGameObjectsPosition.Add(col.transform.position);
        }
        if (endPos == col.transform.position && col.tag == "Empty" && isMoving == true) {
            col.tag = "Occupied";
        }
        if(startPos == col.transform.position && col.tag == "Occupied" && isMoving == true){
            col.tag= "Empty";
        }
    }

and then :

foreach (GameObject obj in myCollidedGameObjects) {

            if(!myCollidedGameObjectsPosition.Contains (obj.transform.position)){
                myCollidedGameObjectsPosition.Add(obj.transform.position);

            }
            }

        foreach (Vector3 positionz in myCollidedGameObjectsPosition) {
            if (Input.GetKey (KeyCode.D)) {
           
                if (transform.position.x+0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {

                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }


            }
            if (Input.GetKey (KeyCode.W)) {
               
                if (transform.position.x == positionz.x && transform.position.y+0.32f == positionz.y && transform.position.z == positionz.z) {
                   
                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }
               
               
            }
            if (Input.GetKey (KeyCode.S)) {
               
                if (transform.position.x == positionz.x && transform.position.y-0.32f == positionz.y && transform.position.z == positionz.z) {
                   
                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }
               
               
            }
            if (Input.GetKey (KeyCode.A)) {
               
                if (transform.position.x-0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {
                   
                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }
               
               
            }

        }

Hmm, here is the full Script, in case that might help. If the previous left anything out.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerMovement : MonoBehaviour {

    public Vector3 StartTile;
    public Vector3 startPos;
    public Vector3 endPos;
    float speed = 2.0f;
    float increment;
    bool isMoving;



    // declare the list
    public List<GameObject> myCollidedGameObjects;

    public List<Vector3> myCollidedGameObjectsPosition;

    // Use this for initialization
    void Start () {

        // initialise the list
        myCollidedGameObjects = new List<GameObject>();
        myCollidedGameObjectsPosition = new List<Vector3>();

        //endPos = transform.position;



        StartTile = GameObject.FindWithTag ("StartTile").transform.position;
        transform.position = StartTile;

        startPos = transform.position;

    }
 
    // Update is called once per frame
    void Update () {

    if (increment <= 1 && isMoving == true) {

            increment += speed / 100;
        } else {
            isMoving = false;
            endPos = transform.position;
        }
        /*if (Input.GetKey (KeyCode.D)&& isMoving == false) {
            increment = 0;
            isMoving = true;
            startPos = transform.position;
            endPos = new Vector3(transform.position.x+0.32f,transform.position.y,transform.position.z);
         
        }*/

        if (isMoving) {
            transform.position = Vector3.Lerp(startPos,endPos,increment);
        }

        foreach (GameObject obj in myCollidedGameObjects) {

            if(!myCollidedGameObjectsPosition.Contains (obj.transform.position)){
                myCollidedGameObjectsPosition.Add(obj.transform.position);

            }
            }

        foreach (Vector3 positionz in myCollidedGameObjectsPosition) {
            if (Input.GetKey (KeyCode.D)) {
         
                if (transform.position.x+0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {

                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }


            }
            if (Input.GetKey (KeyCode.W)) {
             
                if (transform.position.x == positionz.x && transform.position.y+0.32f == positionz.y && transform.position.z == positionz.z) {
                 
                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }
             
             
            }
            if (Input.GetKey (KeyCode.S)) {
             
                if (transform.position.x == positionz.x && transform.position.y-0.32f == positionz.y && transform.position.z == positionz.z) {
                 
                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }
             
             
            }
            if (Input.GetKey (KeyCode.A)) {
             
                if (transform.position.x-0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {
                 
                    increment = 0;
                    isMoving = true;
                    startPos = transform.position;
                    endPos = positionz;
                }
             
             
            }

        }



    }


    void OnTriggerStay2D(Collider2D col)
    {
        // check we don't already have the collided object in the list
        if(!myCollidedGameObjects.Contains(col.gameObject) && col.tag == "Empty")
        {
            // add it to the list
            myCollidedGameObjects.Add(col.gameObject);
            //myCollidedGameObjectsPosition.Add(col.transform.position);
        }
        if (endPos == col.transform.position && col.tag == "Empty" && isMoving == true) {
            col.tag = "Occupied";
        }
        if(startPos == col.transform.position && col.tag == "Occupied" && isMoving == true){
            col.tag= "Empty";
        }
    }
}

Aside from that, the functionality is what I was going for, and was quite happy until I added that extra row/column and things broke.

If the gameobjects aren’t even added to your myCollidedGameObjects List (which when i get it right are the squares your player can move and aren’t blocked by stuff. So if they aren’t added the new sqquares/Gameobjects aren’T maybe aren’t taged as “Empty”.

They are, since I just copy and pasted the other tiles, like I did with setting up the 5x5 grid. Here is a Video :

Around 27/30 seconds, I tried to enter the top right most “Tile” and couldn’t, after shifting my entire “Map”, I could enter it, this is where I become confused.

when you have your map on -0.32 can you enter the first vertical line ?

Yes, and for more testing purposes I moved it to .64, and in doing so I could only move from my “Start Tile”(purple square), up/down and to the left, I was denied moving right. So this makes no sense to me at all. Tossed it to 32, and I could only move straight up/down from my “StartTile”. I don’t see why the position of my map is denying my movement, since how I programmed it was to try and cancel out any alignment problems etc.

The collision works and it adds it to my list, and the x/y are correct, just won’t move to it.

  • Maybe the player is not at a 100% correct position and 0.00001f is missing so position +.32f aren’t equal to your positionz (because of lerp)

  • because when it’s not moving, so what comes after hitting a button,

  • Checking Positions + value → if(transform.position.x+0.32f == positio …

  • isMoving is not set to true

  • after hitting button, is startPos and endPos still the same

  • when yes wy, wrong position saved in list for this tile at this index

make debug logs as needed to get all variables that could fail after they are in list and you hit a key

Here is another video showing the positions :

Also with where it is currently, why can I move to every “Tile” except the one positioned at 1.6,0,0. unless 1.28 + 0.32 doesn’t equal 1.6?

Is my code wrong? If so why does the 5x5 work as I want, but can’t go outside of this?

What are all the reasons in your code that means the object can’t move? Put a breakpoint in at each of those & check what the values are each time. You could be getting an incorrect response for one of the rules that restrict movement & hopefully break points will help identify it as they stop the flow & force you to check everything before you continue.
Otherwise put a debug line in at each of those points so that if ismoving is false it writes to the console, though that won’t help identify at what point or why it was set to false.

Check above link. I ran into this issue and it drove me mad for quite a bit. Particularly because debugging will seem to show you the values you are expecting while the conditional statement fails. For me the direct comparison was the issue

Hmm, I’m new to this stuff, so I have no idea how to “place” debug/breakpoints in. Will try and add the <=/>= within my checks to see if that will work. Also is there any way to deal with “real” or “whole” numbers where floats wouldn’t/shouldn’t cause issues? Thank you.

Hmmm… guess this is outta my league. I have no idea how to fix it, this is why I hate floats and stuff, rather then Ints or the “whole/solid” numbers.

Thank you all for your time!

If you made each grid the size of a Unity grid & placed them at 0.5,0,0.5 & centred the player over it then you could use vector3.forward (or left, right, back etc) to move exactly 1 unit. That may also help with the floating point issues (I did something like that for a variant of the old snake game I made)

Edit: spend some time learning how to use break points. They can be painful but really useful to narrow down where something is going wrong as you can then step through the code line by line looking at the values being passed across etc. I have a love/hate relationship with them

Thanks.