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 BustahNut · Dec 29, 2018 at 01:09 AM · inputtouchcontrolsoverride

How do I override my touch.phase?

I'm working on my mobile game where I want to throw objects by dragging your finger on the screen and releasing them to throw. I also wish to set a constraint on the player that if they drag their finger across a specific yellow line, it will throw the object anyway.

I'm successful with the initial drag and release mechanic but I am unable to get the same results when the object the player is dragging exits a 2D trigger collider. The game still allows the player to drag the object wherever they please and this is not how my game is intended to work.

How can I override a touch.phase so that the object is thrown when exiting a trigger collider? Here's how it is set up. The throw mail function is supposed to add velocity to the object but it still won't do it despite the bool values getting changed by the trigger collider. It still thinks the player is dragging the object despite leftThrowArea changing its value.

 void DragMail()
 {
     if(Input.touchCount > 0 && GameManagerScript.instance.hasGameEnded == false) //if the player touches the screen
     {
         touch = Input.GetTouch(0);
         if (touch.phase == TouchPhase.Began) //at start of touch, has the player touched this mail object?
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
             RaycastHit2D hit = Physics2D.GetRayIntersection(ray, 100);
             if (hit == gameObject)
             {
                 startPos = touch.position;
                 startTime = Time.time;
                 holding = true;
             }               
         }
         if (holding == true && leftThrowArea == false && touch.phase == TouchPhase.Moved) //lets the player move this object around throuch dragging
         {
             Vector2 touchPos = Camera.main.ScreenToWorldPoint(touch.position);
             transform.position = touchPos;
         }            
         //helps determine the time being held          
         if (startTime > 0)
         tempTime = Time.time - startTime;
         if(tempTime > flickTime)
         {
             startTime = Time.time;
             startPos = Input.GetTouch(0).position;
         }
     }        
 }

 void ThrowMail()
 {
     if ((holding == true && touch.phase == TouchPhase.Ended) || (leftThrowArea == true && holding == false)) //throws the mail if the player releases the touch
     {           
         endPos = touch.position;
         swipeDistance = (endPos - startPos).magnitude;
         endTime = Time.time;
         swipeTime = endTime - startTime;
         leftThrowArea = false;
         holding = false;

         if (swipeTime <= flickTime && swipeDistance >= 50f) //throws this mail object
         {
             CalSpeed();
             MoveDir();
             rb.AddForce(new Vector2(direction.x * objectVelocity, direction.y * objectVelocity));                
             thrown = true;
             xScore = 1.5f; //resets score multiplier upon throwing the mail
             score = baseScore; //the score of this mail is reset to base value
         }
         else
         {
             Reset();
         }
     }
 }
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

1 Reply

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

Answer by BustahNut · Jan 01, 2019 at 07:07 PM

Aite so I made some changes in order to get the effect that I was looking for.

Changes: Instead of OnTriggerExit2D I instead swapped the functionality to a OnTriggerEnter2D. This change probably isn't what was necessary in order to get the effect that I was looking for. I changed the entire 2D collider box in order to use the "Enter" function. When the collider was set to be used for the OnExit function I would come across another bug where the player could touch anywhere inside the collider and it would allow the player to drag the object when the player is actually supposed to touch the object specifically. The code is set to only allow for dragging when the player touches the object first.

What fixed my issue: Using boolean variables within IF statements is how you can 'override' the player's screen touches. "Override" is the wrong word to describe what the fix is, but when testing the game I imagined that an override would fix my issue, this isn't the case. Changing the boolean variables from the trigger colliders in conjunction with the IF statements will get the effect I was looking for. I believe this code to be correct. I'm not sure if swapping the trigger collider functionality is what fixed my issue but it has made the difference for me.

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

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

Mobile touching screen is pressing mouse 0 0 Answers

Moving Unity DualTouchControls/Joystick on touch but also functioning on initial touch. 0 Answers

Rotate on Touch Input 1 Answer

The keys W, A, S, D can only make the character move forward. How do I fix this? 0 Answers

Player not moving after build 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