void Move()
{
Vector2 delta = rawInput * (Time.deltaTime * moveSpeed);
transform.position = new Vector2(
// This is all the same line from the one above
Mathf.Clamp(transform.position.x + delta.x,
minBounds.x + paddingLeft,
maxBounds.x - paddingRight),
Mathf.Clamp(transform.position.y + delta.y,
minBounds.y + paddingBottom,
maxBounds.y - paddingTop));
}
The lines are split for readability.
Explanation: As before we assign a delta as it is the only variable we must use more than once. Then instead of assigning new Vector2
to a new variable and then individually assign their x and y, I instead use the Vector2
constructor to assign the x and y with the clamp function inside which is directly assigned to transform.position
.
new Vector2(ClampOne, ClampTwo)
For context, I wouldn’t necessarily recommend this way over the provided way as there are many reasons you may want to assign one time use variables such as readability, easier to debug, and reduces comments. I prefer doing this because I think it is a good way to challenge myself for practice reasons.