Game Development Reference
In-Depth Information
}
}
The following extensions at the end of the current script are just for simplicity, but you
could define them anywhere:
• Convert any Vector3 in to a 2D Vector3 with a zero
z
value
• Convert a screen space coordinate and return a Vector2 for 2D
• Convert a screen space coordinate and return a Vector3 with a zero
z
value for 2D
Note
The
this
keyword signifies that this is an extension method, allowing the function to be
accessed by calling the function normally:
var clickPoint =
WorldExtensions.GetScreenPositionFor2D(Input.mousePosition);
Otherwise, you could directly access the function from the type identified by the
this
keyword:
var clickPoint =
Input.mousePosition
.GetScreenPositionFor2D();
Extension methods are a very powerful and easy way to extend functions and methods on
to existing object types. For more information on extension methods, visit
ht-
Managing input
With our helpers in place, next we'll create a new
MapMovement
script in the project un-
der
Assets\Scripts
and replace its contents with the following:
using UnityEngine;
public class MapMovement : MonoBehaviour
{
Vector3 StartLocation;
Vector3 TargetLocation;
float timer = 0;