Unity 2D 5.2.2 How can I aim with keys towards an object?

Hello there,

It seems that nobody responded to my previous question, it also seems that it was a bit difficult to do then…

But I won’t give up yet!

So, I wanted to ask how to make my main character (Titus) aim at a ‘aim point’ that I control separately.

So basically I want that ‘aim point’ to be controlled with the IJKL keys, but I want to move my Titus with WASD keys, and when I shoot a projectile, it is shot towards my ‘aim point’.

here are my scripts in the following order : Mouse control and shoot and then the movement script of ‘Titus’.

      using UnityEngine;
      using System.Collections;
     public class BulletPrototype1 : MonoBehaviour

    {

public float maxSpeed = 25f;
public GameObject Bullet;

private Transform _myTransform;
private Vector2 _lookDirection;

private void Start()
{
    if (!Bullet)
    {
        Debug.LogError("Bullet is not assigned to the script!");
    }

    _myTransform = transform;
}

private void Update()
{
    /*
    mousePos - Position of mouse.
    screenPos2D - The position of Player on the screen.
    _lookDirection - Just the look direction... ;)
    */

    // Calculate 2d direction
    // The mouse pos
    var mousePos = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

    // Player Camera must have MainCamera tag, 
    // if you can't do this - make a reference by variable (public Camera Camera), and replace 'Camera.main'.
    var screenPos = Camera.main.WorldToScreenPoint(_myTransform.position);
    var screenPos2D = new Vector2(screenPos.x, screenPos.y);

    // Calculate direction TARGET - POSITION
    _lookDirection = mousePos - screenPos2D;

    // Normalize the look dir.
    _lookDirection.Normalize();
}

private void FixedUpdate()
{
    if (Input.GetButtonDown("Fire1"))
    {
        // Spawn the bullet
        var bullet = Instantiate(Bullet, _myTransform.position, _myTransform.rotation) as GameObject;

        if (bullet)
        {
            // Ignore collision
            Physics2D.IgnoreCollision(bullet.GetComponent<Collider2D>(), GetComponent<Collider2D>());

            // Get the Rigid.2D reference
            var rigid = bullet.GetComponent<Rigidbody2D>();

            // Add forect to the rigidbody (As impulse).
            rigid.AddForce(_lookDirection * maxSpeed, ForceMode2D.Impulse);

            // Destroy bullet after 5 sec.
            Destroy(bullet, 5.0f);
        }
        else
            Debug.LogError("Bullet not spawned!");
    }
}

}

     using UnityEngine;
     using System.Collections;

    public class PlayerMovement2 : MonoBehaviour {

    Rigidbody2D PlayerBody;
     Animator Animi;

void Start () {
    PlayerBody = GetComponent<Rigidbody2D>();
    Animi = GetComponent<Animator>();

}

void Update () {
    
    Vector2 movement_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

    if (movement_vector != Vector2.zero)
    {
        Animi.SetBool("Walking", true);
        Animi.SetFloat("Input_x", movement_vector.x);
        Animi.SetFloat("Input_y", movement_vector.y);
    }
    else
    {
        Animi.SetBool("Walking", false);
    }
    PlayerBody.MovePosition(PlayerBody.position + movement_vector * Time.deltaTime);
}

}

Well, those are the two scripts.
Can someone give me some help with this?
I do not want a ‘script that is already made for me’ without information about how it works.
I want an explanation about how to do it, instead of having it all done for me.
Even though I am no great programmer I still want to try it myself!
honestly I do not understand how I can make an object that my ‘Titus’ aims to.
the movement of the object can be done simply, and I can change it in control config in unity game-play settings. The hard part is connecting those two, and make them work correctly.

If you want more information about what kind of game I am making,
It is a 2-D top down platform game (not turn based if it is important to know), so there is no gravity, but there are physics like bouncing, walls, doors and mechanics.
Thank you all in advance and have a nice day,
Daniel Nowak Janssen

I want that ‘aim point’ to be controlled with the IJKL keys, but I want to move my Titus with WASD keys, and when I shoot a projectile, it is shot towards my ‘aim point’.

Ok…

public Transform aimPoint;

Mouse control and shoot

Wait, what? I thought you wanted to aim with IJKL? What else does mouse do than shoot? How many hands do you have, human?

Anyway, movement if aim is simple with IJKL, just set axis up in InputManager.

Vector3 aimAxis = new Vector3(Input.GetAxis("AimHorizontal"), Input.GetAxis("AimVertical"));
aimPoint.Translate(aimAxis * aimSpeed * Time.deltaTime);

If you want to get the direction and rotation to aimPoint, I have two helper classes.

namespace Answers
{
    using UnityEngine;

    public static class Directions
    {
        public static Vector2 FromTo2D(Vector2 from, Vector2 to)
        {
            return (from - to).normalized;
        }

        public static Vector2 FromTo2D(Transform from, Transform to)
        {
            return FromTo2D(to ? (Vector2)to.position : Vector2.zero, 
                from ? (Vector2)from.position : Vector2.zero);
        }
    }

    public static class Rotations
    {
        public static Quaternion LookAt2D(Vector2 from, Vector2 to)
        {
            Vector2 diff = from - to;
            float angle = Mathf.Atan2(diff.y, diff.x) * Mathf.Rad2Deg;
            return Quaternion.Euler(0, 0, angle);
        }

        public static Quaternion LookAt2D(Transform from, Transform to)
        {
            return LookAt2D(to ? (Vector2)to.position : Vector2.zero,
                from ? (Vector2)from.position : Vector2.zero);
        }
    }
}

Then you can do something like…

bulletRotation = Answers.Rotations.LookAt2D(transform, aimPoint);
bulletDirection = Answers.Directions.FromTo2D(transform, aimPoint);

FromTo2D return Vector2 so if you need Vector3, you might need to cast it.

bulletDirection = (Vector3)Answers.Directions.FromTo2D(transform, aimPoint);

Yep, I got it. Thanks again for the help and the explanation.

I still have to test a few things with the mathf.atan2 before I get it completely, but now I at least know the basics, thanks again @Statement!