- Home /
Alternative to using transform.translate and transform.position for moving objects exact values?
Is there a way to move an object a certain amount without using transform.position or transform.translate? I'm working on something that would look like the game Rogue, with ASCII art-esque sprites. I'm using a simple script to make the player move, just using transform.position to move them. However, both transform.position and transform.translate ignore box colliders, like those I've set on the player and the walls in the level I've built. If it would help if I post my code, I definitely can. I just want to know if there was a way to move the player an exact amount that still respects box colliders.
Alternatively, if there isn't a good way of doing this, would there be a way to build this entirely with something like a textmesh? And, in Javascript, how would that work?
Update: I've tried using Rigidbody2D.velocity and such, but it always comes up slightly off, blocking the player from moving into exact areas, and after a while of movement, becoming noticeably off the grid.
Answer by Eudaimonium · Jun 07, 2016 at 12:47 AM
If I'm understanding your problem right, you basically want to move an entire tile's worth of distance at once, no transition in between?
Then you cannot rely on the internal physics engine and you might as well remove all colliders and rigidbodies.
Instead, if you know your world is made up of regular tiles, you can build position and collider arrays.
For example,
bool[][] colliderGrid;
Vector3[][] positionGrid;
At your Start function, generate these arrays according to the tiles present. Then, simply move your player according to indices of the tile you're on and where you want to go. Also check if next tile is a collider according to the same indices.
Then simply use transform.position to move to the precalculated area, instead of using incremental movements which will result in movement error after some time due to nature of floating point numbers.