Ignore players movement when I click on a UI button
Hi, I'm having problems with making my player stay at his last position when I click a UI image, You see I have a click to move script attach to my player and a UI pause image as well. When I click on the image (to pause the game) is pauses but when I resume the game the play goes towards the UI pause image. I tried blocking the player's movement and saving it's last position when the user click on the UI pause image with the code below:
public PlayerMovement playerMovementRef;
public Vector3 lastPosition;
void Start ()
{
PlayerMovement.target = lastPosition;
}
void Update ()
{
OnMouseUp ();
}
public void OnMouseUp(){
if (!EventSystem.current.IsPointerOverGameObject ()) {
lastPosition = transform.position;
playerMovementRef.enabled = false;
}
}
But it's not really working, because when I click on my pause button (UI pause image) and then resume my game(by clicking the resume button) my player still goes towards the the pause button (UI pause image), rather than saving the player last position and staying there well until like I move my player again.
This is my player's movement script:
public float speed = 1.5f;
private Vector3 target;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.fixedDeltaTime);
}
(P.s. I need fixedDeltaTime for something else in my game)
I also did this but it stop me from moving my player when I play the game:
if (Input.GetMouseButtonDown (0)) {
if (!EventSystem.current.IsPointerOverGameObject ()) {
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
}
}
I think I know the problem behind the the script above isn't working. I think it's because the image background is a canvas panel. This is because I want the game background to be locked in place just in case if the game screen is stretch out of if someone has a large phone then the game background can stay in place. (my game is a top down view 2d game)
Thank you in advance. :)
Your answer
Follow this Question
Related Questions
Remove button OnMouseOver highlighting 0 Answers
Is it possible to take input from 2 UI buttons? 2 Answers
How to move character with UI buttons? 0 Answers
Trouble with GUI Button Level switching. 0 Answers
Create a custom button 0 Answers