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();
}
}
}
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.
Your answer
Follow this Question
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