2D Game - Move To Position and then Do Something.
Hello,
I am making a game that requires a toon to be moved from point a to point b via tap on a mobile device.
The scene has things like Trees, Berries, Water and stuff like that. When the player taps on one of these resources, i want the toon to walk over to said resource, mine and then the resource would then be put into the users "Inventory". I have the resources generating right now via click. But now i want to take it a step further and introduce some movement with a sprite toon.
I've done youtube videos, unity answers and the other site that is for questions but everyone gets bent out of shape if you ask a question(I left right away, cant remember the name).
This is the ONLY script i could find online that moves something from a click, but not even close to the right way.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class Player : MonoBehaviour { public float Speed;
private Vector3 Target;
private void Start()
{
Target = transform.position;
}
private void FixedUpdate()
{
if (Input.GetMouseButtonDown(0))
{
Target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, Target, Speed * Time.deltaTime);
}
}
First thing i don't like is i have no clue whats going on with this script. I don't like writing stuff out that doesn't make sense to me. If anyone has the time and want to explain, please feel free to do so. If not, i get it.
Second, this script is the only one that caused some kind of movement that i have tried. The problem is, regardless of what speed i put into the editor, the toon will just go straight to the bottom left corner and then disappear. Does not follow the click.
Third, no clue how to incorporate (do this) once the object gets to it's destination. I picture a bunch of if statements but my ignorance is winning on this one. First time i've asked a question on Unity.
Any help is greatly appreciated! :D