I'm new to unity and I'm doing the RogueLike 2D tutorial. By default the player will move, theoretically , using one of these coordinates per turn y+1, x+1, y-1, x-1
, or just one block at a time.
And I want to know how I can get the player to move freely until they hit a wall or other collider in the scene.
Like the game 2048 , where the squares advance in a straight line and only stop when they collide on the wall or in other parts.
Below is the part of the code that states the move. What changes should I make to leave the movement as I mentioned above?
//Co-routine for moving units from one space to next, takes a parameter end to specify where to move to.
protected IEnumerator SmoothMovement (Vector3 end)
{
//Calculate the remaining distance to move based on the square magnitude of the difference between currente position and end parameter.
//Square magnitude is used instead of magnitude because it's computationally cheaper.
float sqrRemainingDistance = (transform.position - end).sqrMagnitude;
}
//While that distance is greater than a very small amount (Epsilon, almost zero):
while(sqrRemainingDistance > float.Epsilon)
{
//Find a new position proportionally closer to the end, based on the moveTime
Vector3 newPosition = Vector3.MoveTowards(rb2d.position, end, inverseMoveTime * Time.deltaTime);
//Call MovePosition on attached Rigidbody2D and move it to the calculated position.
rb2d.MovePosition (newPosition);
//Recalculate the remaining distance after moving.
sqrRemainingDistance = (transform.position - end).sqrMagnitude;
//Return and loop until sqrRemainingDistance is close enough to zero to end the function
yield return null;
}
-
If you can easily understand the question, this code was provided by unity and can be downloaded here .