Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
Efficient way to detect touch except on the area of joystick

I'm using this joystick and Mirror for networking

https://github.com/herbou/Unity_EasyJoystick

And I have this script to fire a bullet on the user touch

public class ShootBullet : NetworkBehaviour
{
    [SerializeField]
    private GameObject bulletPrefab;
    private GameObject bullet;

    // Set via the Inspector in Units/second
    [SerializeField] private float _moveSpeed = 2f;
    [SerializeField] private Camera _camera;
    Vector3 touch_Pos = new Vector3(0, 0, 0);

    private void Awake()
    {
        if (!_camera) _camera = Camera.main;
    }

    void Update()
    {
        if (Input.touchCount > 0 && this.isLocalPlayer)
        {
            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
                var touch = Input.GetTouch(0);
                touch_Pos = _camera.ScreenToWorldPoint(touch.position);

                this.CmdShoot();
            } 
        }

        if (bullet != null)
        {
            bullet.transform.position = Vector3.MoveTowards(bullet.transform.position, touch_Pos, _moveSpeed * Time.deltaTime);
        }
        
    }

    [Command]
    void CmdShoot()
    {
        bullet = Instantiate(bulletPrefab, this.transform.position, Quaternion.identity);
        NetworkServer.Spawn(bullet);
    }

}

My problem right now is it's firing a bullet when I touch the joystick area. What is the efficient way to exclude input.touch on the joystick area

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can raycast touch position and check the result.

var mousePos = Input.mousePosition;
var mouseToRay = _camera.ScreenPointToRay(mousePos);
RaycastHit hit;
if (Physics.Raycast(mouseToRay, out hit))
{
    var gameObject = hit.collider.gameObject;
    //Here You can check the name of your object or the layer or tag
    var isMouseUnderJoyStick = gameObject.CompareTag("Your_UI_Tag");
}

And you have to add collider component to your JoyStick

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda