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
  • \$\begingroup\$ Welcome. "Fix this code for me" style of questions are more suitable for a forum and rarely get answers here. Code for touch api and gamepad aren't interchangeably. Code for touch api and mouse input events may be interchangeably (with some care). For gamepad, you need a new block of code, that rotates an aiming vector, 2D in this case, shoots will be in the direction of that vector. I recommend this video tutorial. \$\endgroup\$ Commented Oct 24, 2020 at 2:59
  • \$\begingroup\$ We have some past Q&A about configuring a touch script to ignore the part of the screen reserved for the on-screen joystick, which may help you here. \$\endgroup\$ Commented Oct 24, 2020 at 21:09

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.