- Home /
C# to UnityScript conversion help.
So I am trying to translate a A* path finding script(a part of it at least) to UnityScript. I have gotten it to run without errors, but not run correctly. I is supposed to make a grid covering the area of the cube(in my case it is an invisible square covering my level. To test out if it works before continuing on, I made it so that it instantiates cubes. It ended up making four rows of stair-like figures. Can you guys please look at my code and tell me what I am missing?
http://puu.sh/2AZuk http://puu.sh/2AZuQ
Mine:
class GridCell
{
var cellWalkable: boolean;
var cellHeight: float;
//Width and height of the map (in cells)
var mapWidth: int;
var mapHeight: int;
}
var cellSize: float = 0.5;
var bounds: Bounds;
var topLeftCorner: Vector3;
//Map of the world
var gridCells = new Array();
var square: GameObject;
function Start ()
{
bounds = renderer.bounds;
}
function Update ()
{
//Scans the bounds of the model, then creates a grid
//Finds the top left corner
topLeftCorner = bounds.center - bounds.extents + Vector3(0, bounds.size.y, 0);
//creates the grid map
var cells = GridCell();
//Calculates the dimensions of the grid map.
cells.mapWidth = Mathf.RoundToInt(bounds.size.x / cellSize);
cells.mapHeight = Mathf.RoundToInt(bounds.size.y / cellSize);
//Scans for walkable terrain in each cell
for(var x = 0; x < cells.mapWidth; x++)
{
for(var y = 0; y < cells.mapHeight; y++)
{
//Gets the position for a ray
var currentPosition = topLeftCorner + Vector3(x * cellSize, 0, y * cellSize);
var hit: RaycastHit;
//Creates a cell for the grid
var cell = new GridCell();
//Cast the ray
if(Physics.Raycast(currentPosition, Vector3(0, -1, 0),hit, bounds.size.y))
{
//the height of the highest item in the cell
cell.mapHeight = hit.point.y;
//Test if the hitpoint is walkable
Instantiate(square, hit.point, Quaternion.Euler(0,0,0));
if(((1 < hit.collider.gameObject.layer) /*&& walkableLayer*/) !=0)
{
//Flag the cell we hit as walkable
cell.cellWalkable = true;
}
}
}
}
}
His: He has a site that he posted this on http://unitygems.com/astar-1-journeys-start-single-step/ To be more specific it is the part in this picture. http://puu.sh/2AZCy
THANK YOU!
It looks to me like square has a box collider on it. In that case it will hit the box when it tests the next cell. Also there's no need for the if(((1 > hit.collider.gameObject.layer code if you aren't testing for a specific walkable layer you could just make the cell walkable immediately.
I recommend though:
You put the walkable parts of the level on their own layer.
You specify a walkable layer property and set it to the layer (that way you won't be able to walk on enemies, trees etc).
Ok, so removing the collider fixed that issue, but It is still not going through the whole area. Also, I haven't added the separate layer yet, so some of the cells do appear on the tops. I am guessing it is a problem with either mapHeight or mapWidth. Thanks.
Also, is there a better way to debug this. I am currently displaying the grid cells and that causes Unity to usually freeze. Is there another way?
Oh wow. Now I feel stupid. I was not understanding why the SQUARES (not cells) were not spawning far away from each other, when the code is only set to make the CELLS spawn far away from each other. lol
Oh wait. It was actually the fact that I had it measure the distance on the wrong axis. I was thinking of the 3dsmax y axis. To anyone who wants to know.
Your answer
Follow this Question
Related Questions
Help with conversion from javascript to c# 3 Answers
javascript equivalent of Action? 0 Answers
convert some c# to javascript please 0 Answers
Convert Javascript to C# 1 Answer
Need help converting Javascript to C# 3 Answers