var waypoint : Transform;
//I dont want to code it like this
var waypoint2 : Transform;
var speed : float = 2;
function Start ()
{
}
function Update ()
{
OnMouseDown();
}
function OnMouseDown()
{
if(Input.GetMouseButton(0))
{
//Can i reference the waypoint.position code for all waypoints instead of just the one?
transform.position = Vector3.MoveTowards(transform.position, waypoint.position, speed*Time.deltaTime);
}
//I dont want to use this code
if(Input.GetMouseButton(1))
{
transform.position = Vector3.MoveTowards(transform.position, waypoint2.position, speed*Time.deltaTime);
}
}
This is sample code for a project im working on. I have a player and waypoints, in this example, i have two.
What i would like is for the player to move toward the object i click on without reusing code like i have above. The code above works fine but i know its not efficient. I have all waypoints tagged with “waypoint”.
Is there a way i can accomplish this?