addForce to rigidbody into the direction of a specific object

Hey guys,

I am just wondering how to accomplish this effect. I have a rigidbody and I have a target, the rigidbody is changing position from time to time. Now at a specific point I want to add a force to the rigidbody which is aiming at that specific target no matter where the rigidbody is located at the time.
Anyone has done that before or knows how to do that?

Thanks in advance.

-zem

rigidbody.AddForce (GameObject.Find("target").transform.position.x, GameObject.Find("target").transform.position.y, GameObject.Find("target").transform.position.z);

Just tested it, it works :slight_smile: Cheers!

That will just make it go random speeds depending on the distances between them. What you want is the the normalised vector between them, which you can multiply back up to the speed you desire?

Using GameObject.Find is terrible for performance too. Wait until you get bigger scenes.

True I thought of the normalized vector as well but wasn’t bothered to write the code down lol.
How else would you find a gameObject?? Unless of course you have it defined in the code as a GameObject right?

Hmm, just look for a simple seek behavior

Direction = (target.position - myTransform.position).normalized;

Thos will give you the direction to aim at

Then as hippo said use this vector with whatever coefficient you want as desired speed amount.

And better to cache your target before in a start fct

1 Like

hey thanks guys, I have setup a specific tracking system with smart objects, so the game object is always knowing where the desired target is. just required that portion, I will give it a go with the normalized vector, thanks. :slight_smile:

hmm okay strange, I guess I have to play with the values a bit, but for some reason its always shooting into one direction (down mostly).
What I am trying to achieve is an effect like this shooting barrels here: http://www.youtube.com/watch?v=Asryeyig1_Q

the code is almost working but for some reason its not shooting the player up, even if the target is up.

public class ShootingBarrel : MonoBehaviour 
{
	private GameObject hamster;
	private bool isAttached = false;
	private Transform target;
	
	void Start()
	{
		this.target = this.gameObject.transform.FindChild("Target").transform;
	}
	
	// player is inside the barrel
	void OnTriggerEnter(Collider other)
    {
        if (other.tag.Equals("Player"))
		{
			this.hamster = other.gameObject;
			this.isAttached = true;
			this.hamster.rigidbody.velocity = Vector3.zero;
		}
    }
	
	void Update()
	{
		if(this.isAttached)
		{
			this.hamster.transform.position = this.transform.position;
			
			if(Input.GetKeyDown(KeyCode.L))
			{
				this.hamster.rigidbody.AddForce((this.target.position - this.gameObject.transform.position).normalized * 50);
				this.isAttached = false;
			}
		}
	}
}

bump :slight_smile: still havn’t found a solution :frowning: I am not sure why it isn’t shooting in the proper direction :confused:

okay now its flying up sometimes, but for some reason it still isn’t 100% the correct direction

Sorry 7 years later I found this thread useful. Really helped me get the answer in a super simple way. So thank you! Thought I’d help fill in the last missing piece for @ @sebako maybe this is what was missing?

    [SerializeField] private GameObject gameObj;
    [SerializeField] private GameObject gameObj2;
    [SerializeField] private Rigidbody2D _rigid2d;
    [SerializeField] private float _value;
    [SerializeField] private float _value2;

   void Update ()
    {
        if (Input.GetKeyDown(KeyCode.H))
        {
            _rigid2d.AddForce((gameObj.transform.position - gameObj2.transform.position).normalized * _value);
            _rigid2d.AddForce(transform.up * _value2);
        }
    }