Search Unity

Movement Issues

Discussion in 'Scripting' started by Impervion, Oct 6, 2015.

  1. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    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?
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    how does the code you with which you set up ur lists and you set up your map?
     
  3. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    Code (CSharp):
    1. void OnTriggerStay2D(Collider2D col)
    2.     {
    3.         // check we don't already have the collided object in the list
    4.         if(!myCollidedGameObjects.Contains(col.gameObject) && col.tag == "Empty")
    5.         {
    6.             // add it to the list
    7.             myCollidedGameObjects.Add(col.gameObject);
    8.             //myCollidedGameObjectsPosition.Add(col.transform.position);
    9.         }
    10.         if (endPos == col.transform.position && col.tag == "Empty" && isMoving == true) {
    11.             col.tag = "Occupied";
    12.         }
    13.         if(startPos == col.transform.position && col.tag == "Occupied" && isMoving == true){
    14.             col.tag= "Empty";
    15.         }
    16.     }
    and then :
    Code (CSharp):
    1. foreach (GameObject obj in myCollidedGameObjects) {
    2.  
    3.             if(!myCollidedGameObjectsPosition.Contains (obj.transform.position)){
    4.                 myCollidedGameObjectsPosition.Add(obj.transform.position);
    5.  
    6.             }
    7.             }
    8.  
    9.         foreach (Vector3 positionz in myCollidedGameObjectsPosition) {
    10.             if (Input.GetKey (KeyCode.D)) {
    11.            
    12.                 if (transform.position.x+0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {
    13.  
    14.                     increment = 0;
    15.                     isMoving = true;
    16.                     startPos = transform.position;
    17.                     endPos = positionz;
    18.                 }
    19.  
    20.  
    21.             }
    22.             if (Input.GetKey (KeyCode.W)) {
    23.                
    24.                 if (transform.position.x == positionz.x && transform.position.y+0.32f == positionz.y && transform.position.z == positionz.z) {
    25.                    
    26.                     increment = 0;
    27.                     isMoving = true;
    28.                     startPos = transform.position;
    29.                     endPos = positionz;
    30.                 }
    31.                
    32.                
    33.             }
    34.             if (Input.GetKey (KeyCode.S)) {
    35.                
    36.                 if (transform.position.x == positionz.x && transform.position.y-0.32f == positionz.y && transform.position.z == positionz.z) {
    37.                    
    38.                     increment = 0;
    39.                     isMoving = true;
    40.                     startPos = transform.position;
    41.                     endPos = positionz;
    42.                 }
    43.                
    44.                
    45.             }
    46.             if (Input.GetKey (KeyCode.A)) {
    47.                
    48.                 if (transform.position.x-0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {
    49.                    
    50.                     increment = 0;
    51.                     isMoving = true;
    52.                     startPos = transform.position;
    53.                     endPos = positionz;
    54.                 }
    55.                
    56.                
    57.             }
    58.  
    59.         }
     
  4. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    Hmm, here is the full Script, in case that might help. If the previous left anything out.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public Vector3 StartTile;
    7.     public Vector3 startPos;
    8.     public Vector3 endPos;
    9.     float speed = 2.0f;
    10.     float increment;
    11.     bool isMoving;
    12.  
    13.  
    14.  
    15.     // declare the list
    16.     public List<GameObject> myCollidedGameObjects;
    17.  
    18.     public List<Vector3> myCollidedGameObjectsPosition;
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.  
    23.         // initialise the list
    24.         myCollidedGameObjects = new List<GameObject>();
    25.         myCollidedGameObjectsPosition = new List<Vector3>();
    26.  
    27.         //endPos = transform.position;
    28.  
    29.  
    30.  
    31.         StartTile = GameObject.FindWithTag ("StartTile").transform.position;
    32.         transform.position = StartTile;
    33.  
    34.         startPos = transform.position;
    35.  
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update () {
    40.  
    41.     if (increment <= 1 && isMoving == true) {
    42.  
    43.             increment += speed / 100;
    44.         } else {
    45.             isMoving = false;
    46.             endPos = transform.position;
    47.         }
    48.         /*if (Input.GetKey (KeyCode.D)&& isMoving == false) {
    49.             increment = 0;
    50.             isMoving = true;
    51.             startPos = transform.position;
    52.             endPos = new Vector3(transform.position.x+0.32f,transform.position.y,transform.position.z);
    53.          
    54.         }*/
    55.  
    56.         if (isMoving) {
    57.             transform.position = Vector3.Lerp(startPos,endPos,increment);
    58.         }
    59.  
    60.         foreach (GameObject obj in myCollidedGameObjects) {
    61.  
    62.             if(!myCollidedGameObjectsPosition.Contains (obj.transform.position)){
    63.                 myCollidedGameObjectsPosition.Add(obj.transform.position);
    64.  
    65.             }
    66.             }
    67.  
    68.         foreach (Vector3 positionz in myCollidedGameObjectsPosition) {
    69.             if (Input.GetKey (KeyCode.D)) {
    70.          
    71.                 if (transform.position.x+0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {
    72.  
    73.                     increment = 0;
    74.                     isMoving = true;
    75.                     startPos = transform.position;
    76.                     endPos = positionz;
    77.                 }
    78.  
    79.  
    80.             }
    81.             if (Input.GetKey (KeyCode.W)) {
    82.              
    83.                 if (transform.position.x == positionz.x && transform.position.y+0.32f == positionz.y && transform.position.z == positionz.z) {
    84.                  
    85.                     increment = 0;
    86.                     isMoving = true;
    87.                     startPos = transform.position;
    88.                     endPos = positionz;
    89.                 }
    90.              
    91.              
    92.             }
    93.             if (Input.GetKey (KeyCode.S)) {
    94.              
    95.                 if (transform.position.x == positionz.x && transform.position.y-0.32f == positionz.y && transform.position.z == positionz.z) {
    96.                  
    97.                     increment = 0;
    98.                     isMoving = true;
    99.                     startPos = transform.position;
    100.                     endPos = positionz;
    101.                 }
    102.              
    103.              
    104.             }
    105.             if (Input.GetKey (KeyCode.A)) {
    106.              
    107.                 if (transform.position.x-0.32f == positionz.x && transform.position.y == positionz.y && transform.position.z == positionz.z) {
    108.                  
    109.                     increment = 0;
    110.                     isMoving = true;
    111.                     startPos = transform.position;
    112.                     endPos = positionz;
    113.                 }
    114.              
    115.              
    116.             }
    117.  
    118.         }
    119.  
    120.  
    121.  
    122.     }
    123.  
    124.  
    125.     void OnTriggerStay2D(Collider2D col)
    126.     {
    127.         // check we don't already have the collided object in the list
    128.         if(!myCollidedGameObjects.Contains(col.gameObject) && col.tag == "Empty")
    129.         {
    130.             // add it to the list
    131.             myCollidedGameObjects.Add(col.gameObject);
    132.             //myCollidedGameObjectsPosition.Add(col.transform.position);
    133.         }
    134.         if (endPos == col.transform.position && col.tag == "Empty" && isMoving == true) {
    135.             col.tag = "Occupied";
    136.         }
    137.         if(startPos == col.transform.position && col.tag == "Occupied" && isMoving == true){
    138.             col.tag= "Empty";
    139.         }
    140.     }
    141. }
    142.  

    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.
     
  5. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    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".
     
  6. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    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.
     
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    when you have your map on -0.32 can you enter the first vertical line ?
     
  8. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    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.
     
    Last edited: Oct 6, 2015
  9. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    The collision works and it adds it to my list, and the x/y are correct, just won't move to it.
     
  10. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    - 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
     
  11. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    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?
     
  12. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    Is my code wrong? If so why does the 5x5 work as I want, but can't go outside of this?
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
  14. ury2ok2000

    ury2ok2000

    Joined:
    Dec 29, 2012
    Posts:
    46
  15. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    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.
     
  16. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    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!
     
  17. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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
     
  18. Impervion

    Impervion

    Joined:
    Oct 1, 2015
    Posts:
    44
    Thanks.