- Home /
BoardGame like movement. From tile to tile movement
Hi, I am learning Unity by porting board game to pc. Issue: Player can move by tiles on particular distanse. I would like to make this movement to be step by step from tile to tile. Right now there is code to find path and to get array of tiles that player will go trough. I've tried to make it in CustomCharacterController
by it is still moving imidiatly to final tile.
[UsedImplicitly]
public class CustomCharacterController : MonoBehaviour {
[SerializeField]
private float _moveSpeed = 0.5f;
private bool _isMoving = false;
private Vector3 _nextTarget;
[UsedImplicitly]
private void Start() {
}
[UsedImplicitly]
private void Update() {
}
public void MoveToBySteps(Square[] characterPathToTarget) {
for (int i = 1; i < characterPathToTarget.Length; i++) {
_isMoving = true;
Square next = characterPathToTarget[i];
Coordinates? nextCoordinates = next.ParentTile.FindSquareCoordinates(next);
if (!nextCoordinates.HasValue) { continue; }
float targetX = nextCoordinates.Value.X + next.ParentTile.X + 0.5f;
float targetY = nextCoordinates.Value.Y + next.ParentTile.Y + 0.5f;
_nextTarget = new Vector3(targetX, targetY, 0);
while (_isMoving) {
Move();
}
}
}
private void Move() {
float step = _moveSpeed;
transform.position = Vector3.Lerp(transform.position, _nextTarget, step);
if ((transform.position - _nextTarget).magnitude <= 0.05f) {
_isMoving = false;
}
}
}
Also here can be found other bits of code: here
Answer by MaxGuernseyIII · Nov 20, 2017 at 09:36 PM
I would look in to animation. Create an animation and and animation controller. Attach them to your tile. Use the animation editor to make the movement look as smooth as you want. Then have your movement controller tell the animation where to move the tile from and to and initiate an animation.
The problem of making it move through multiple squares in sequence can be handled with a coroutine. loop through each of the spots to which you want to hop but yield while the animation for movement is playing.
Information on animations and coroutines are available in the manual.
Can I somehow do this without animations? Do I really need it in order to move? This is just simple move A-B-C-D $$anonymous$$y issue is that visually it is just A-D and momentally.
$$anonymous$$ax's advice is good, whether or not you are using animations. Another way to say what you want is: move to first square, wait N seconds, move to next square, wait N seconds... You have written the movement code but you aren't waiting so the player sees nothing.
The good news is, it will be straightforward to turn $$anonymous$$oveBySteps into a co-routine. After every step your coroutine just needs to yield for N seconds, and then your loop will continue. There's a little more to it than that, but not much more.
I thought part of the problem was you wanted the pieces to move smoothly from one space to another. If you don't want that, you don't need animations.
If you do want to do it, you can still hand-roll the smooth movement but you would just be reinventing animations at that point.
O$$anonymous$$, thanks. Will try to make whole $$anonymous$$oveToBySteps
as coroutine as suggested.
Your answer
Follow this Question
Related Questions
An arrow pointing from one hexTile to another hexTile 0 Answers
Object Squeezing Between Tiles 0 Answers
Problem using a position determined from onTrigggerEnter in Update 2 Answers
Creating a 2D Movement Grid 0 Answers
Raycast based tile navigation 0 Answers