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 $$anonymous$$ · Aug 22, 2017 at 08:07 PM · scripting problemmovement

How can I let stuff reset in the script when touching a button

Sorry if I couldn't explain that right, what I want to know is how can I make a few lines of code reset or go back to how it was when touching something.I'm using OnPointerDown and OnPointerUp triggers so I made my game so you press on the right side of the screen and it moves right and the same for left so both triggers for each side , I made another Button and putted it in the middle of the other buttons (middle of the screen) so when you slide over it when you want to go the other way it would reset the OnPointerDown and OnPointerUp but it doesn't work like that it would go in one direction if you press the buttons to fast. Here is the code. So is there anyway to reset the OnPointers so it doesn't bug out when swiping across the screen to move?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Movement : MonoBehaviour {
 
     public void MoveLeftButton(string moveleft)
     {
         Player.horizVel = -3;
 
     }
 
     public void MoveRightButton(string moveright)
     {
         Player.horizVel = 3;
 
     }
 
     public void MoveLeftButtonStop(string moveleftstop)
     {
         Player.horizVel = 0;
 
     }
 
     public void MoveRightButtonStop(string moverightstop)
     {
         Player.horizVel = 0;
 
     }
 
 
 
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by UnityCoach · Aug 22, 2017 at 08:39 PM

You could, and probably should, rather use IDragHandler.OnDrag, over the whole area, and use the pointer drag Data position to move and or reset based on the coordinates.

Comment
Add comment · Share
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
avatar image
1

Answer by tablekorner · Aug 22, 2017 at 09:05 PM

I'm going to assume that you have seperate scripts for each directional button. Or some way to differentiate between which direction to go for both buttons.

There isn't any need to reset OnPointerDown, and OnPointerUp as the events are designed so the actions in them are only happing if Pointer is down, or if Pointer is up.

Keep in mind that OnPointerUp will also work in the scenerio where you pressed down on a button, dragged off the button while holding down your press, then released off of the button, this is viable to still call OnPointerUp. If this is not the functionality you want, then I'd suggest looking into OnPointerEnter, and OnPointerExit instead. Or a combination of OnPointerEnter, OnPointerExit, and OnPointerDown.

My suggestion would be to have a script on your buttons with this:

 public bool mouseOff = false;
 
 public void OnPointerEnter (PointerEventData eventData)
 {
     mouseOff = true;
 }
 
 public void OnPointerExit (PointerEventData eventData)
 {
     mouseOff = false;
 }
 
 public void OnPointerDown (PointerEventData eventData)
 {
     mouseOff = true;
 }

This would allow you to check if button is pressed, or if you press off button, and drag into it. But will also ensure to return false if you press, and drag off the button.

Then you could do sometihng using your functions like this:

 if (someObject.GetComponent<WhateverScriptHasOnPointerFunctions>().mouseOff)
 {
     if (someObject.name == "SomeButtonLeft")
     {
         // Call your MoveLeftButton function here
     }
 }
 else
 {
     if (someObject.name == "SomeButtonLeft")
     {
         // Call your MoveLeftButtonStopfunction here
     }
 }
Comment
Add comment · Share
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

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

137 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 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

Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers

Unity Space Shooter Tutorial Player Will Not Move 1 Answer

Script problems c# 0 Answers

How to stop movement script on void start and resume after. 0 Answers

Rigidbody2D movement is lagging 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