2D Aiming WITHOUT mouse...?

Hi all!
I have made a 2D Shooter but want to use ControlPad instead of Mouse when I’m aiming.
I’ve tried to google “2D Aiming Unity” etc but can’t find any tutorial on how to implement 2D Aiming with keyboards/controller… It’s all for mouse…
Does anyone have any clue what I should search or what to do?
Do I need raycast or something like that, or could I simply Replace the mouse with the right analog stick?

My current script looks like this, but I want to add “If Controller = True” do this instead…

using UnityEngine;
using System.Collections;

public class AimScript : MonoBehaviour

{
    public Transform hitPoint;
    public bool direction;
    public PlayerController p_Script;
    public Camera cam;
    public int posOffset;

    void Awake()
    {
        cam = GetComponent<Camera>();
    }

    void Start()

    {
        direction = true;
        cam = GameObject.Find("MainCamera").GetComponent<Camera>();
    }

    void Update()

    {


        if (p_Script.aimingKnife || p_Script.aimHandGun || p_Script.aimingShootGun || p_Script.aimingRifle)
        {
            Vector3 mousePos = Input.mousePosition;
            mousePos.z = 5f;
            //rotation

            Vector3 objectPos = cam.WorldToScreenPoint(transform.position);

            mousePos.x = mousePos.x - objectPos.x;
            mousePos.y = mousePos.y - objectPos.y;

            float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;


            if (direction == true) { posOffset = 0; }
            if (direction == false) { posOffset = -180; }

            transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle + posOffset)); //Rotating!



            if (angle > 0f && angle < 100f || angle < 0f && angle > -90f)
            {
                if (direction == false)

                {
                    direction = true;

                    p_Script.FlipToRight();
                    Flip();
                }
            }

            if (angle > 100f && angle < 180f || angle < -90f && angle > -180f)

                if (direction == true)

                {
                    direction = false;

                    p_Script.FlipToLeft();
                    Flip();
                }
        }

    }

    void Flip()

    {
        hitPoint.Rotate(Vector3.forward * 180);
        Vector3 theScale = transform.localScale;
        theScale.x *= -1;
        transform.localScale = theScale;
    }

}

Just replace the mouse direction with the analog stick as it is already a direction.

1 Like