Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by wasicool7 · Aug 28, 2016 at 05:30 PM · 2duibuttonplayer movement

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. :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

115 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges