rb.AddForce() is launching my player in the opposite direction of the vector 2 value I assigned

I’m making a game where you click and drag with the mouse then release to launch the player. But sometimes the player gets launched in the opposite direction of where it should go. I made a debug output to show you the different values. 203001-error.png

In that image for example you can see that the Vector2 of force * power is positive on the y-axis, but the player launched downwards, and the same happens Vice versa. I think it’s also worth noting that this happens inconsistently for some reason. Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public GameObject player;
    public float power = 10f;
    public Rigidbody2D rb;
    public float maxSpeed;

    public Vector2 minPower;
    public Vector2 maxPower;

    TragectoryLine tl;

    Camera cam;
    public Vector2 force;
    public Vector3 startPoint;
    public Vector3 endPoint;
    public Vector3 currentPoint;
    public Vector3 startPointMouse;

    public bool isPulling = false;
    float distance;



    private void Start()
    {
        cam = Camera.main;
        tl = GetComponent<TragectoryLine>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            startPointMouse = cam.ScreenToWorldPoint(Input.mousePosition);
            startPointMouse.z = 15;
        }
        if (Input.GetMouseButton(0))
        {
            startPoint = player.transform.position;
            startPoint.z = 15;
            isPulling = true;
            Vector3 currentPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            currentPoint.z = 15;
            tl.RenderLine(startPoint, currentPoint);

          
        }

        if (Input.GetMouseButtonUp(0))
        {
            endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
            endPoint.z = 15;
            isPulling = false;
            
            tl.EndLine();
            distance = startPointMouse.magnitude - endPoint.magnitude;

            if (distance < 0)
            {
                distance = -distance;
            }
            if (distance >= 1)
            {
                rb.AddForce(force * power, ForceMode2D.Impulse);
            }


            force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));

            Debug.Log("distance" + distance);
            Debug.Log("start" + startPoint);
            Debug.Log("end" + endPoint);
            Debug.Log("force" +force);
            Debug.Log("force * power" + force * power);
        }

    }

    private void FixedUpdate()
    {
        rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
    }
}

Here I added the force using rb.AddForce(force * power, ForceMode2D.Impulse); when the force * power value was positive on the y axis. So why did it go to the opposite direction???

This was working perfectly fine before i tried implementing a feature where the player has to move the mouse a certain distance or else it wont launch. I have tried removing it but it doesnt seem to make a difference. I think I changed something in the code that ruined it but I cant figure out what! Please help!

You calculate the force after it has already been added. So my guess is that it is using the force of your previous drag.
Right now the scripts does things in the following order:


  1. Adds force
  2. Calculates the new force
  3. Writes force into console

While the force you write in the console is the one you have calculated, it is not the force you have added to your rigid body.

Move the calculation of force to before you do rb2D.AddForce