Search Unity

2D sidescroller aim system help needed

Discussion in '2D' started by Chaos119, Jan 2, 2015.

  1. Chaos119


    Apr 14, 2013

    I'm working on a 2D sidescroller and I'm trying to decide on how I should go about creating the aiming system for launching projectiles. This is the first time I'm trying to tackle something like this so I'm slightly in doubt as to how to go about achieving it. I've linked an image below with what I have in mind.
    The general idea is that holding down right mouse button shows the circle and mouse position indicator and also displaying the path the projectile will travel. So this is what I'm trying to achieve:
    • I would like to have the mouse pos indicator to follow the "orbit" around the player.
    • Display the trajectory that the projectile will travel on. Possibly with "bullet drop".
    • Making the mouse pos indicator face the right direction to make sure I can instantiate a projectile and have it fly along the projected path.
    As I said, this is the first time I'm trying to go about creating a weapons system that works like this so I'm in need of some help :p I'm not looking for finished scripts rather some guidelines that could point me in the right direction :)

    P.S. Projectile wise, I'd assume that using physics to launch projectiles will make it harder to calculate the trajectory due to it adding the velocity of the players movement onto it if I use AddForce. Would it be better to move the projectile by using a different method?

  2. wizardious


    Dec 31, 2009
    Maybe you could have an empty rotate around the player based on the mouse position?
    I found this script that might help get you started.
    Code (JavaScript):
    1. var HitObj : GameObject;
    2. var Distance  = 0f; //in case you didnt know f means float
    3. function Update () {
    4. var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up);
    5. // Making a new ray that will go straight down from the gameobject the code is attached to
    6. Debug.DrawLine (transform.position, hit.point,;
    7. //Debugging is optional but I like to see the red raycast line and it makes it easy  to see what it's hitting
    8. Distance = Mathf.Abs(hit.point.y - transform.position.y);
    9. // grabbing the distance in a 2d plane AS A FLOAT
    10. }