public void MoveByX(int dir, float speedMultiply = 1, Command.CommandExecuted executedCallback = null) { Vector2 vel = Vector2.zero; vel.x = dir * movementStats.moveSpeed * speedMultiply; vel.y = Velocity.y; body2d.velocity = vel; }
public IEnumerator MoveTo(Vector2 pos, float speedMultiply = 1, Command.CommandExecuted executedCallback = null) { Vector2 distance = pos - position; Vector2 dir = distance.normalized; // calculate velocity Vector2 vel = dir * movementStats.moveSpeed; while (Vector2.Distance(pos, position) >= 0.01) { // start moving with entity's speed in calculated direction // ***** this line of code has to be in the coroutine excecution block ***** // body2d.velocity = vel; yield return(null); } // movement finished, reset velocity body2d.velocity = Vector2.zero; }
public IEnumerator MoveBy(float x, float y, float speedMultiply = 1, Command.CommandExecuted executedCallback = null) { // calculate distance Vector2 distance = Vector2.zero; distance.Set(x, y); // move direction Vector2 dir = distance.normalized; // destination pos Vector2 pos = position; pos.x += x; pos.y += y; // calculate velocity Vector2 vel = dir * movementStats.moveSpeed * speedMultiply; while (Vector2.Distance(pos, position) >= 0.01) { // start moving with entity's speed in calculated direction // ***** this line of code has to be in the coroutine excecution block ***** // body2d.velocity = vel; yield return(null); } // movement finished, reset velocity body2d.velocity = Vector2.zero; if (executedCallback != null) { executedCallback(); } }
public void MoveByY(int dir, Command.CommandExecuted executedCallback = null) { Vector2 velocity = Vector2.zero; velocity.x = dir * movementStats.moveSpeed; }