Point and Click character movement
Hi, I'm new to unity and coding and have been trying to figure out how to make simple point and click controls for the game I want to create. This is the code that I have currently.
public float walkSpeed;
public Vector2 goHere;
private Rigidbody2D Player;
void Start ()
{
Player = GetComponent<Rigidbody2D>();
walkSpeed = 5;
}
void Update ()
{
var mouseButtonLeft = Input.GetKeyDown(KeyCode.Mouse0);
if(mouseButtonLeft)
{
goHere = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2.MoveTowards(transform.position, goHere, 0.0f);
Player.velocity = goHere * walkSpeed;
}
if (goHere == Player.position)
{
Player.velocity = Vector2.zero;
}
My problem with it is that when I click in the game the character moves towards the mouse, but does not stop moving and if I click more he only goes faster.
All I want the code to do is
move the character to where the mouse clicks in the game at a consistent speed, no matter how many times the floor is clicked. Then, once the character gets to where the mouse has clicked last stop moving.
only move when the floor in the game is clicked.
I haven't even tried to get the second part of that to work yet and I know that the code is probably incomplete even for the first part, but I have spent many hours trying to figure all of this out on my own and can't seem to get it.
In anyone has any idea of how to get this to work I'd really appreciate it.
Your answer
Follow this Question
Related Questions
Scale sprite-animated character in 2D to fake distance (Monkey Island 3 style) 0 Answers
Moving forward seems to be specific to the original direction of the object being controlled 0 Answers
unity 2d movement problem 0 Answers
Mouse controlled 2D object bleeding through walls 1 Answer
How do I transfer the momentum from my speed to the player? 0 Answers