Help finding rotation between two points

So I’m building a 2D platformer and I’m at a blockage trying to get one of my weapon types to work. It’s supposed to be like shooting lightning. It makes a beam that splits into two beams that split again, rinse repeat until the max range is hit. I’m having trouble drawing the beams.

What my code does is it uses a linecast to see if it hits anything. If it does, it sets ‘lineEnd’ to the point where it hit. If not, ‘lineEnd’ stays the same random point. The sprite is set to the center point of the line and scaled to be the distance of the line. What I need now is to find the angle of my linecast so the sprite rotation can be set. Otherwise, it just looks like a horizontal line that’s been slightly shifted.

    //Object to instantiate
    public GameObject nextshot;

    //Beam collider
    Collider2D beam;

    //Linecast positions
    Vector2 lineStart;
    Vector2 lineEnd;

	// Use this for initialization
	void Start () {
        //Set segment angle and distance
        lineStart = transform.position;
        lineEnd.x = lineStart.x + Random.Range(10f, 15f);
        lineEnd.y = lineStart.y + Random.Range(-10f, 10f);

        
        makeBeam();

    }
	

    //Linecast to find hits and call function to draw beam
    void makeBeam ()
    {
        RaycastHit2D hit = Physics2D.Linecast(lineStart, lineEnd);
        if (hit.collider != null)
        {
            lineEnd = hit.point;
        }
        drawBeam(lineStart, lineEnd);

    }


    //Draw the beam
    void drawBeam (Vector2 a, Vector2 b)

    {
        Debug.DrawLine(lineStart, lineEnd);
        transform.position = a + (b - a) * 0.5f;
        float dist = Vector2.Distance(a, b);
        transform.localScale = new Vector2(dist + 1, 1);

    }
}

I need to find the rotation angle of the line that goes between points ‘lineStart’ and ‘lineEnd’ so that I can set the rotation of my sprite.

Hey, from the sounds of things you’re wanting to work out an angle between 2 Vector2’s, right? Here’s some ways for doing that.


If you’re using Unity 2017 or newer, you can use Vector2.SignedAngle, this takes in 2 Vector2’s and returns a float that represents the angle between these 2 objects. The ‘signed’ part means it can be negative as well as positive, so SignedAngle ranges from -180 to 180 to represent the angle between the 2 Vector2’s.


float angle = Vector2.SignedAngle(position1,position2); // Returns a value between -180 and 180.

Obviously replace position1 and position2 with the positions you want to check the angles of (Vector2’s). Docs link for SignedAngle: Unity - Scripting API: Vector2.SignedAngle


Unfortunately, if you are using Unity 5.x or earlier, this function isn’t available, but there is Vector2.Angle. While SignedAngle returns the, well SignedAngle, Angle returns the UnsignedAngle. The simple explanation is this one will not go below 0, but will still only go up to 180, it will return the smallest value possible of that, so while SignedAngle might return say -90 (if it’s a 270 degree angle) Angle would just return 90.


float angle = Vector2.Angle(position1,position2); // Returns a value between 0 and 180.

Again, replace position1 and position2 with the positions you want to check the angles of (Vector2’s). Docs link for Angle: Unity - Scripting API: Vector2.Angle


Hope that helps!