0
\$\begingroup\$

Hello everyone i am really stuck at the moment and would appreciate any help if possible. I have my game where i move the player up and down with 2 buttons on the bottom left of screenshot.

I also have a button over the entire screen for tap to shoot and this moves the weapon to my finger position where i have tapped and it shoots towards that position using mouse.position.

Now the problem is when i am on my android device and i am moving up and down but when i also tap to shoot the gun it moves towards my up and down buttons and this is not what i want.

i want to be able to move with my finger and also shoot towards the second finger.

Any ideas would be greatly appreciated.

Here is my Tap to shoot button code!

public void ShootButton()
{
 timer = 0;
 Face = true;
 //if finger presses Shootbutton
 //then activate this

 Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - 
 transform.position; 
 // subtracting the position of the player from the mouse position.
 difference.Normalize(); //normalizing the vector meaning all the sum of the 
 vector will be = to 1.

 float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;//find 
 the angle in degree.

 WeaponTrans.transform.rotation = Quaternion.Euler(0f, 0f, rotZ + 
 rotationOffset);
 if (reloading == false)
 {
     if (fireRate == 0)
     {
         {
             Shoot();
         }
     }
     else
     {
         if (Time.time > timeToFire)
         {
             timeToFire = Time.time + 1 / fireRate;
             Shoot();
         }
     }
 }
}

enter image description here

\$\endgroup\$
0
\$\begingroup\$

I´m not sure what you exactly meant. But I guess you mean on a touchscreen. And your weapon jumps around once you click any of the up & down buttons. 1) use Input.Touch instead of Input.mousePosition 2) use a foreach loop like this:

foreach(Touch t in Input.Touches)
 {
   //now you have different posibilities:
   //one of them could be 
   if(t.position is inside of the button)
   {
     //do not use it for detecting direction
   }
   else
   {
     //the touch position is not inside a button => execute shoot code
   }
 }

also, you said you have a button over the entire sceen to detect touches: That´s bad practise you can use touch phases like this:

switch(touch.phase)
{
  case Touchphase.Began:
  //code executed when the finger touches the screen
  break;
  case Touchphase.Ended:
  //code executed when the finger leaves the screen
  break;
}

I guess that takes to long to explain but there are tons of good videos out there. This one might explain it good.

If you have more questions feel free to comment and ask on specific. I might able to answer more specific then

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.