How to add force on an object in the direction in which the player is facing?

I’m using character controller so I’m using :

private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.gameObject.GetComponent<Rigidbody>() != null)
            hit.gameObject.GetComponent<Rigidbody>().AddForce(Vector3.forward);
    }

But it’s adding force only in certain direction, I’ve cans (cylinders), I want to make them fall in the direction I’m facing when I push them.

Use the player’s .forward

Vector3.forward returns (0,0,1), that’s forward relative to the world

1 Like

I’ve tried it, not working.

1 Like

Try harder. It’ll work.

11 Likes

oh my god!.. still not working.

6 Likes

What did you try?
Show us your new code

1 Like

You need to use Rigidbody.AddRelativeForce not Rigidbody.AddForce.

Also, the “new” keyword is incorrect here although I’m sure you’ve spotted Unity complaining about that already.

36 Likes

Could also do AddForce(Player.transform.forward)

4 Likes

Yes, this is working, thanks!

I just had to change Player.transform.forward to this.transform.forward, how is it different from Vector3.forward?

‘new’ was a typo (I didn’t paste the code here, directly typed it), edited the question and fixed. transform.forward is working for me.

Sure but it’s slower as this gets the transform then computes a world-space forward vector which can then be passed to the world-space AddForce. Using AddRelativeForce requires the constant forward vector only and be applied directly.

In short; AddForce requires a world-space vector whereas AddRelativeForce requires a local-space vector.

11 Likes

Answered it on the first post

True that, but performance shouldn’t be a concern for him yet(assuming you’re new to unity), he should mostly realize what does what and why

I agree and understanding local-space and world-space difference is super important too hence my post.

Anyway, lots of useful info here, thanks.

3 Likes

I used this:

rb.AddRelativeForce(Vector3.forward);

It’s not working.

2 Likes

Actually I’d appreciate it If I get any advice which leads to a more smooth and optimised game.

Well it does work so not sure how you’ve got that setup there. It might be more useful to describe what is happening rather than simply saying it does not work. Will maybe give us clues to what is going wrong on your side.

Here’s a simple test project. Focus on the cube, press play and rotate its Transform around the Y axis in the inspector. It’ll add force to the direction its facing. Note the force obviously accumulates so if you leave it too long in one direction it’ll get quite fast and will resist changes in motion.

https://oc.unity3d.com/index.php/s/XyvGZWBZFW0P67q

1 Like

Maybe the force is just too weak?

Your project has the force acting the moving player itself, My project is a little different, here’s the simplified version of my project, you can just open and click play, you’ll notice the problem:

https://drive.google.com/drive/folders/1ZIUubTfUHlzOFX8yZ9wywWpN0WpYGcQs?usp=sharing

I’ve a force multiplier, I’ve tried different values, the force is acting but not in the direction I want.