- Home /
How do I prevent players from turning unless they are in the center of a tile?
I have a physics-based game, that is at this point mostly finished. However, the game plays out on a grid and I'd like to keep players in the center of the grid tiles. I know the spacing and number of tiles and can apply that, and my movement/engine code is already finished:
void LateUpdate() {
float throttle = Input.GetAxis("Vertical");
if (Input.GetAxisRaw("Horizontal") < 0)
{
StartCoroutine(RotateObject(new Vector3(0, 270, 0), 0.4f));
}
if (Input.GetAxisRaw("Horizontal") > 0)
{
StartCoroutine(RotateObject(new Vector3(0, 90, 0), 0.4f));
}
Vector3 basePosistion = Vector3.forward;
if (throttle > 0)
{
basePosistion = rigidbody.rotation * basePosistion * throttle;
} else {
basePosistion = rigidbody.rotation * basePosistion * 0;
}
rigidbody.velocity = basePosistion * AmbientSpeed;
captureTest();
}
How would I determine where the player is on a tile? My suspect feeling is that I can use a magnitude test to prevent them from turning, but other than that I am not sure.
Answer by Owen-Reynolds · Dec 07, 2012 at 03:53 PM
Use standard mapping functions to go from tile# to worldPos, and worldPos to tile#. Say your tiles are 5 units wide. That means the center of tile#X is at world 5X+2.5. The other way, from world to tile, is (worldX-2.5)/5.
Check that: Tiles are centered at 2.5, 7.5, 12.5, 17.5... . if you are at 14, you are on the right side of the 10-15 tile (which is #2). Math gives you (14-2.5)/5 = 2.3. Meaning you on tile#2, right side.
The "best" numbers are whole numbers, meaning you are dead center of a tile The worst end in 0.5, meaning you are 1/2-way between two tiles. So just check your tile# is "close enough" to a whole number. If you do it for X and Z, you'll get a square that counts as "in the center." For a circle, could square the results instead.
In this case my tiles are 14 units in size. I have the current tile via the formula position.x/y / spacing, or transform.position.x / 14. That gives me the edge as an int. So to get the correct magnitude for the center, I'd add + 7 to a float?
Try any formula, get some paper and test out some numbers.
If you want to just divide by 14, like you say, whole numbers are the edges. So check the fraction is "about 0.5". If you'd rather check feet, take the fraction times 14. That will give you how many feet from the edge.
float xTest = transform.position.x / gameController.spacing; I currently have this to test for tiles, but it gives me mostly whole numbers. .spacing is = 14. It seems to be almost the formula you described.
"$$anonymous$$ostly whole numbers"? Are you saying that at 21, it correctly gives you 1.5, but then when you move to 26 it suddenly rounds up to 2? $$anonymous$$eep testing, pause (near tile center & at edge) and hand-check values.
If you're using `Debug.Log` it often rounds to the nearest tenth, so standing at 42.5 might print 3.0.
I misspoke, the version in the control had (int) cast. Anyway.. this isn't working to produce what I need. I'm looking at other methods such as false pressure plates now.
Your answer
Follow this Question
Related Questions
Can someone help me to find what I am doing wrong?(code attached) 1 Answer
How to find a circle (radius) in grid tiles around another tile 1 Answer
Correct use of setTile to add tile to Tilemap 0 Answers
Texture grid displayed oddly when width =/= height 1 Answer
Bad grid rigibody3D collision 1 Answer