- Home /
Touch Inputs Not Allowing Mutiple Presses
Hey guys, I've been trying to sort this problem out for a couple of days now, I had help from FifthKnotch about a similar problem 2 days ago, since then I've got my code nearly working fine on mobile, the user can do everything I want them to do, but they can't do any of them at the same time.
count = Input.touchCount;
foreach ( Touch touch in Input.touches) {
if (moveUp.HitTest(touch.position)) {
{
//Move Up
}
}
else
{
//Stop Animations & Sound
}
if (moveLeft.HitTest(touch.position)) {
//Spin Left
}
if (moveRight.HitTest(touch.position)) {
//Spin Right
}
}
if (Input.GetKey(KeyCode.Space)&&Time.time>nextShotTime)
{
nextShotTime = Time.time+fireInterval;
if (GameObject.FindWithTag("Player"))
{
GameObject go = GameObject.FindWithTag("Player");
Vector3 newPos = new Vector3(-0.3f,0.0f,0.0f);
newPos = go.transform.TransformPoint(newPos);
GameObject projectile = Instantiate(Shot, go.transform.position, go.transform.rotation) as GameObject;
Physics.IgnoreCollision(projectile.collider, go.collider);
Rigidbody rb = (Rigidbody)projectile.GetComponent("Rigidbody");
rb.velocity = go.transform.TransformDirection(Vector3.left) * 10;
}
}
I hope that's not too much code for you, I removed the actual code's after the IF statements because I don't feel they're relevant, but if you need them, just ask. I left in the shoot code at the bottom because that one is having the most bugs
I did get the fire button to work on mobile devices using a similar code to the rest, but I couldn't figure out how to do a Time.time>nextShotTime with the touch input
TLDR: I need to have all of my touch inputs able to work with each-other and I need to work out how to shoot in the parameters of nextShotTime
Thank you in advanced for anyone who can help me, I'm pretty desperate!
Answer by fifthknotch · Mar 11, 2014 at 04:33 AM
Hello again. The reason your script is not multi-touch is because you are using the same variable "touch" for every button. Touch can ONLY have one value at a given time. Because of this, the first touch is assigned for all buttons. If you make separate scripts all with a touch variable for checking if your touch is on their assigned button, this should solve your problem.
Huh.. ok thanks, how exactly would that look? And would that still work to control the same object if it was all in different scripts?
Split these:
if (moveUp.HitTest(touch.position)) {
{
//$$anonymous$$ove Up
}
}
else
{
//Stop Animations & Sound
}
and
if (moveLeft.HitTest(touch.position)) {
//Spin Left
}
and
if (moveRight.HitTest(touch.position)) {
//Spin Right
}
and so on, into individual scripts, each checking its own touch input. That way it can change as more fingers are added.
Alright I'll give that a go, just to clarify though, I have this code above each individual script:
count = Input.touchCount;
foreach ( Touch touch in Input.touches) {
And then do I link it to the prefab of my ship or the button itself?
Yes. $$anonymous$$eep that so your scripts keep up with the touch inputs. $$anonymous$$eep these scripts on the same object you had them on.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135933im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Touch buttons to replace Input GetKey KeyCode 1 Answer
Input.GetAxis("Vertical") on touch devices. 2 Answers
Touch Input help.. 1 Answer
TouchPhase not firing correctly - Android 3 Answers
How to touch a specific cube 1 Answer