Question about AddForceAtPosition

I have a cube and two smaller ones representing “propulsion rockets”. I’d like to apply force to each one in order to simulate a hovering effect. I managed to ApplyForce from the main cube to another rigidbody (smaller cube), but I can’t seem to understand how to use AddForceAtPosition. Here’s what I got:

void FixedUpdate ()
	{
	
		if (Input.GetAxis("Vertical") <0)
		{		
			//engine = GameObject.FindWithTag("CenterLeftThruster");
			//engine.rigidbody.AddForce(0,12,0);
			
			engine = GameObject.FindWithTag("CenterLeftThruster");
		  	Vector3 direction = engine.transform.position - transform.position;
			engine.rigidbody.AddForceAtPosition(direction.normalized, transform.position);
		}
	}

Thanks!

The second parameter to AddForceAtPosition should be engine.transform.position - the position value for this function works in world space, not in the local space of the rigidbody you are applying the force to.

Thanks Andeee. You’re saying if I use engine.transform.position, it would apply force based on world position, not local? Because that’s what I believe is happening on my second try.

Force is applied to the object but in the same axis, regardless if the object flips over. I’m guessing it’s applying force based on world position. I’d like for it to apply force down one axis.

I’ve tried reading the documentation, but personally having trouble understanding the concept behind AddForcePosition :frowning:

Second Try:

void FixedUpdate ()
	{
	
		if (Input.GetAxis("Vertical") <0)
		{		
			
			engine = GameObject.FindWithTag("CenterLeftThruster");
		  	//Vector3 direction = engine.transform.position - engine.transform.position;
			engine.rigidbody.AddForceAtPosition(new Vector3(0, 20, 0), engine.transform.position);
		}
	}

If you want to apply the force on the local Y axis then you can use something like

engine.rigidbody.AddForceAtPosition(-engine.transform.up * 20f, engine.transform.position);
2 Likes

That worked perfectly, thanks again. I have a better understanding of AddForceAtPosition now.