0
\$\begingroup\$

Hi I am new to unity and have only a little coding knowledge. Right now I have made my character shoot at my touch position on mobile but when ever I touch the joystick, whick I use to move my character, he shoots there, what should I do to make him not shoot at the joystick when i hold the joystick, any suggestion... heres my code.

Shooting code

public class Shooting : MonoBehaviour
{
    public GameObject shot;
    private Transform playerPos;

    void Start()
    {
        playerPos = GetComponent<Transform>();
    }
    void Update()
    {

            if (Input.GetTouch(0).phase == TouchPhase.Began)
            {
             Instantiate(shot, playerPos.position, Quaternion.identity);
            }
            else if (Input.GetTouch(1).phase == TouchPhase.Began)
            {
                Instantiate(shot, playerPos.position, Quaternion.identity);
            }
        else if (Input.GetTouch(2).phase == TouchPhase.Began)
        {
            Instantiate(shot, playerPos.position, Quaternion.identity);
        }


    }
}

Projectile Code

public class Projectile : MonoBehaviour
{
    private Vector2 target;
    public float speed;
    private Shake shake;
    void Start()
    {
        shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>();
        target = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
        target = Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
        target = Camera.main.ScreenToWorldPoint(Input.GetTouch(2).position);
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);

        if(Vector2.Distance(transform.position, target) < 0.2f)
        {
            Destroy(gameObject);
        } 
            
        
    }
    void OnTriggerEnter2D(Collider2D other)
    {
        shake.CamShake();
    }
}

Thanks in advance

\$\endgroup\$
2

Your Answer

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

Browse other questions tagged or ask your own question.