- Home /
C# Detect Touch without delay
Hey, i'm using this code to check Right and Left clicks:
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began){ Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began){ if (touch.position.x > (Screen.width/2)){ rigidbody2D.AddForce (Vector2.up*300); rigidbody2D.AddForce (Vector2.right*150); rigidbody2D.velocity = new Vector2(0.0f,0.5f); transform.rotation = Quaternion.Euler(0,0,0); } if (touch.position.x <= (Screen.width/2)){ rigidbody2D.AddForce (Vector2.up*300); rigidbody2D.AddForce (Vector2.right*-150); rigidbody2D.velocity = new Vector2(0.0f,0.5f); transform.rotation = Quaternion.Euler(0,180,0); } } }
But when it detects a touch it has a delay to check the next touch, in my project I need to remove this delay so that the person can touch the right and quickly after touching the left.
Answer by OtunGroup · Sep 24, 2018 at 01:21 AM
Hi, this script is so baffling. You've tested for TouchPhase.Began twice, and repeated multiple lines of code unnecessarily as well.
I don't know if this works. Feel free to try it.
if (Input.touchCount > 0 && Input.GetTouch(Input.touchCount - 1).phase == TouchPhase.Began)
{
Touch t = Input.GetTouch(Input.touchCount - 1);
bool leftOrRight = t.position.x > Screen.width / 2;
rigidbody2D.AddForce(Vector2.up * 300);
rigidbody2D.AddForce(Vector2.right * leftOrRight ? 150 : -150);
rigidbody2D.velocity = new Vector2(0.0f, 0.5f);
transform.rotation = Quaternion.Euler(0, leftOrRight ? 0 : 180, 0);
}
Hope this helps or at least points you in the right direction. -Greg
Sorry, I have not yet learned to touch Touch and am very confused about it. I used the code and the following errors appeared:
-Operator '*' cannot be applied to operands of type 'UnityEngine.Vector2' and 'bool'
-The best overloades method match for 'UnityEngine.RigidBody2D.AddForce(Unity.Engine.Vector2)' has some invalid arguments
-Argument #1' cannot convert
object' expression to type `UnityEngine.Vector2'
-The left-hand side of an assignment must be a variable, a property or an indexer
Can you solve it?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Touch phase is moved when touch pressure changed 0 Answers
Rotate with two fingers 0 Answers
Make object follow my finger (Touch)? 1 Answer
Multiple Cars not working 1 Answer