Hi, I’m trying to figure out how to script a waypoint system (i.e., when you want a unit to flank rather than head straight for the last click) for my 2D characters.
Right now, I have a simple script that causes my character to move toward the last click. What I need is the ability to hold down shift and click to create waypoints. With this, the character will travel to the first click (while shift was held down) before heading to the second.
I’ve been trying to figure out how to code this, but I’m having some trouble as I am a beginner. If someone could help me by explaining how to create this system for waypoints-on-click (while holding shift), that’d be extremely helpful. Right now I’ve been making flow charts and brainstorming to see if I could use arrays, etc, but I’m pretty stumped lol.
Thank you for any help.
---------------------------- This is the code for the current movement system.
public class moveOnMouseClick : MonoBehaviour
{
public float moveSpeed; // adjustable movement speed of character
private Vector2 destinationPosition; // The destination Point (Click)
private Vector2 myPosition; // current position of my character
void Start ()
{
myPosition = transform.position; // sets position to the transform.position
}
void Update () {
if (Input.GetMouseButtonDown(0)){ // checks for mouse click
destinationPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); // sets mouse position as target destination
}
transform.position = Vector2.MoveTowards(transform.position, destinationPosition, moveSpeed * Time.deltaTime); // moves unit toward destination
}
}
I’d also like to mention that the current movement code is also up for critiquing. I’m not sure if that’s the best/easiest way to get move-on-click movement, etc. Also, right now the character always moves to the center of the screen at the beginning of the game. I’m not sure lmao. It’s only at the beginning; he moves to the center once. After that it works like normal. I can also click while he’s moving to the center (the origin, I mean) and he’ll move fine and not try going to the center.
I am such a beginner. =P