Instatiated Object addForce not working as expected, taking up player velocity as value

I have a player object that instantiates a simple 3d sphere. The script I have here is intended to add force in the direction the player is facing, however the sphere is instantiated and just sits there in mid air.

        if (Input.GetMouseButtonDown(1))
        {
            Instantiate(projectile, firePoint.transform.position, this.transform.rotation);
            projectile.GetComponent<Rigidbody>().AddForce(this.transform.rotation.eulerAngles * 10);
        }

This is where you go through the debug process. Ratchet up the force to see if its too low. Check the vector is pointing where you expect it to.

Check the documentation for the AddForce method: Unity - Scripting API: Rigidbody.AddForce

You should probably look at using a difference force mode as I feel like the force you’re applying is far too low.

1 Like

Thank you I will try this

        if (Input.GetMouseButtonDown(1))
        {
            Instantiate(projectile, firePoint.transform.position, this.transform.rotation);
            projectile.GetComponent<Rigidbody>().AddForce(this.transform.rotation.eulerAngles * 100, ForceMode.Force);
        }

The above code did not change the behavior.

The instantiated sphere object’s collider is set to be a trigger, may this be the issue? I guess I could simply click the box and test this, however I like to keep the discussion going until someone may have something clever because I have learned very valuable tricks from easy fixes on these forums. Good community.

EDIT: Okay, so if I press the mouse button when I am walking, then it has velocity… so my code is taking my own velocity as the force’s value, so I guess I have to pass a different value… I thought the * X coefficient value would add that speed and that the eulerAngle code would simply give my sphere the same direction as the player is facing…

Okay, so I figured out that I need to write it like follows, but now the problem is the sphere shoots straight up in the air and does not take the rotation of the player and the intended direction is forward…

        Vector3 shootDirection = transform.rotation.eulerAngles;
        if (Input.GetMouseButtonDown(1))
        {
            GameObject projectile1 = Instantiate(projectile, firePoint.transform.position, this.transform.rotation, this.transform);
            projectile1.GetComponent<Rigidbody>().AddForce(shootDirection * 2, ForceMode.Force);
        }