Click here to Skip to main content
15,881,810 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a 2D Platformer and I am tring to add mobile controls

I am trying to make it so when you touch on the joystick the player will start shooting.

What I have tried:

I tried to change all of the input.getAxsis with CrossPlatformInputManger.getAxsis but all it did was make me not able to shoot. I also changed the build setting to IOS and made sure mobile input is enabled

Here is the code

void Update ()
    {
        if (fireRate == 0)
        {
            if (CrossPlatformInputManager.GetButtonDown("Fire1"))     
            {
                Shoot();          
            }
        }
        else
        {
            if (CrossPlatformInputManager.GetButton ("Fire1") && Time.time > timeToFire) 
            {
                timeToFire = Time.time + 1 / fireRate;
                Shoot();
            }
        }
    }


And the Shoot Method

void Shoot ()
    {
        Vector2 joyStickPosition = new Vector2(Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition).x, Camera.main.ScreenToWorldPoint(CrossPlatformInputManager.mousePosition).y);
        Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
        RaycastHit2D hit = Physics2D.Raycast (firePointPosition, joyStickPosition-firePointPosition, 100, whatToHit);

        Debug.DrawLine(firePointPosition, (joyStickPosition - firePointPosition)*100, Color.cyan); 
        if (hit.collider != null)
        {
            Debug.DrawLine(firePointPosition, hit.point, Color.red);
            Enemy enemy = hit.collider.GetComponent<Enemy>();
            if (enemy != null)
            {
                enemy.DamageEnemy(Damage);
                //Debug.Log("We hit " + hit.collider.name + "and did " + Damage + "damage");
            }
        }
Posted
Updated 21-Feb-17 19:26pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900