Addforce To an object forward a Player

Hi. I have an issue trying to make an AddForce to a object in Unity3D. I want that object to be pushed in front of my character, but the AddForce is allways applied in the same direction, not in the Player.Forward position. Heres the part of the code :

public Transform Player;
private Rigidbody rigidboxx;

void Update(){
if (Input.GetKeyDown("g") && (objactual.tag == "Grabbable"))
        {
            rigidboxx.AddForce(Player.forward * forceV);
           
        }
}

This code should work. Make sure you are assigning the player object to the Player field instead of something else.

1 Like

Everything seems to be ok, but still not working, the objects just going in another direction

A small snippet of code isn’t a way to figure out what is going wrong. You need to debug it.

You can verify what “Player.forward” is. You can verify what “forceV” is and what the result is when multiplied. You can stop adding the force to see if it stops moving and it’s not something else moving it such as collision or another script etc.

Adding a single force might not do much, the force you specify will be time-integrated. A one-off “force” is typically done as an impulse which you can specify as the second argument to “AddForce”.

1 Like