- Home /
help with a player moving in a grid with obstacles 2d
- Hi. My player, a rabbit, should move with WASD in a grid. Below it is my code, sorry I put it all, but I want to be sure that in the end all will be fine. I'm new to C# and Unity. I know Java and C if you want to explain me something. 
- Here is a short description on what it does: randomly generating how many objects in the scene I need and storing their positions in two matrices(works); picking carrots(works); if in an adjacent position there is something according to the occupiedObstacles matrix, the rabbit should not move(but it does not move at all). Until I merged together two files(one with the generating, other with the control of the rabbit) the rabbit was moving(but it was acting strangely with obstacles; i chose to give up that way). However, here is a pastebin with the two files and a video with the rabbit acting weird(after I captured this video, i tried to change the radius of the box collider, still no succes 
 https://pastebin.com/NgqtWVEa https://youtu.be/10y4BDnhadQ I have to mention, the tiles have positions like x:0.48 y: -2.801942, maybe the problem comes from here?- Blockquote 
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { Vector3 pos; 
 float speed = 1.0f; 
 private Rigidbody2D rb;
 [SerializeField] int requiredCarrots = 20;
 [SerializeField] GameObject carrotPrefab = null;
 [SerializeField] int requiredStones = 5;
 [SerializeField] GameObject stonesPrefab = null;
 [SerializeField] int requiredTrees= 5;
 [SerializeField] GameObject treesPrefab = null;
 [SerializeField] int requiredCaves = 5;
 [SerializeField] GameObject cavesPrefab = null;
 public bool[,] occupiedCarrots = new bool[10, 8]; // 2D array of bools to store if a position is occupied with a carrot
 public bool[,] occupiedObstacles = new bool[10, 8];//... with an obstacle
 void Start () {
     pos = transform.position;          
     if (carrotPrefab == null || requiredCarrots > 10 * 8) { return; } // protect against some errors
     if (stonesPrefab == null || requiredStones > 10 * 8) { return; }
     if (treesPrefab == null || requiredTrees> 10 * 8) { return; }
     if (cavesPrefab == null || requiredCaves > 10 * 8) { return; }
     occupiedCarrots[0, 7] = true; // rabbit position (in reality it is -4.5, 3.5)
     occupiedObstacles[0, 7]=true;
     GenerateObjects(requiredCarrots,occupiedCarrots,carrotPrefab);
     GenerateObjects(requiredStones,occupiedObstacles,stonesPrefab);
     GenerateObjects(requiredTrees,occupiedObstacles,treesPrefab);
     GenerateObjects(requiredCaves,occupiedObstacles,cavesPrefab);
 }
 void GenerateObjects(int numberRequired, bool[,] occupiedPositions, GameObject objectPrefab){
     for (int i = 0; i < numberRequired; ++i) {
         int x, y; 
         do {
             x = UnityEngine.Random.Range (0, 10); 
             y = UnityEngine.Random.Range (0, 8);             
         } while (occupiedCarrots[x, y] || occupiedObstacles[x, y]);
         if (!objectPrefab.Equals (carrotPrefab)) { // avoid cases when the rabbit is stuck at the very beginning
             if (!(((x == 1 && y == 7) && occupiedPositions [0, 6].Equals (true)) ||
                 ((x == 0 && y == 6) && occupiedPositions [1, 7].Equals (true))))
                 occupiedPositions [x, y] = true;
             Instantiate (objectPrefab, new Vector3 (x - 4.5f, y - 3.5f, 0), UnityEngine.Quaternion.identity); 
         } else {
             occupiedPositions [x, y] = true;
             Instantiate (objectPrefab, new Vector3 (x - 4.5f, y - 3.5f, 0), UnityEngine.Quaternion.identity); 
         }
     }
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if(other.gameObject.CompareTag("Pickup")) 
     {
         Destroy (other.gameObject);  
     }
 }
     
 void FixedUpdate () {
     
     if(Input.GetKey(KeyCode.A) && transform.position == pos && occupiedObstacles[(int)pos.x-1,(int)pos.y].Equals(false)) {      
             transform.Translate(-1.0f, 0.0f, 0.0f);
             transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * 2*speed); 
         }
     if (Input.GetKey (KeyCode.D) && transform.position == pos && occupiedObstacles[(int)pos.x+1,(int)pos.y].Equals(false)) {       
             transform.Translate (1.0f, 0.0f, 0.0f);
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * 2 * speed);
         }
     if (Input.GetKey (KeyCode.W) && transform.position == pos && occupiedObstacles[(int)pos.x,(int)pos.y+1].Equals(false)) {       
             transform.Translate (0.0f, 1.0f, 0.0f);
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed); 
         }
     if (Input.GetKey (KeyCode.S) && transform.position == pos && occupiedObstacles[(int)pos.x,(int)pos.y-1].Equals(false)) {        
             transform.Translate (0.0f, -1.0f, 0.0f);
             transform.position = Vector3.MoveTowards (transform.position, pos, Time.deltaTime * speed); 
         }       
 }
 void Update(){
     pos = transform.position;
 }
}
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                