Hi! I am new to Unity and I found this script online for moving the player using mouse input. But every time the level starts the player moves to the center of the scene. I’m not sure how to keep it where I placed it during setup. Please help! (Unity 2018.2.7f1, 2D)
My code:
public class MovePlayer2 : MonoBehaviour {
public Vector2 targetPosition;
[SerializeField]
private Rigidbody2D rb;
[SerializeField]
private float speed = 10;
// Update is called once per frame
void FixedUpdate () {
if(Input.GetMouseButtonDown(0)) { //checks that the left mouse button has been pressed
targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); //sets the targetPosition to the dimensional point in the game where the mouse clicked
}
transform.position = Vector2.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed); //it will move from the current position to the target position and the time.deltaTime
}
}