Adding Force towards an Object

Hiya and thanks for looking into my post :slight_smile: I am working on a sort of 3rd person shooter/action game and am having a problem with getting my projectiles to fire in the desired direction. As it isn’t a fps I couldn’t just add a GUI cross hair so I decided I would add a plane object to my Mouse Orbit that works as a cross hair.

There is a shooting spawnPoint located just infront of the characters right hand that I have setup with a LookAt script to always face the cross hair. I have it working that the Rockets rotate towards their destination but I haven’t been able to find anywhere how to Force them towards it.

var rocketPrefab:Transform;

function Update () 
{
	if(Input.GetButtonDown("Fire1"))
	{
		var rocket = Instantiate(rocketPrefab, 
										GameObject.Find("spawnPoint").transform.position, 
										GameObject.Find("spawnPoint").transform.rotation);
		rocket.rigidbody.AddForce(transform.forward *2000);
		
	}
}

I know this is probably a very simple problem and I have tried searching around the wiki and google but to no avail :frowning:

If anyone can help me it would be much appreciated

:slight_smile: Sir MaXx

You didn’t specify exactly what problem you’re having, but generally, you’d apply forces and so on in FixedUpdate(), not Update(). Also, applying a force only once may not have the desired effect (for a rocket, you’d typically want to apply the force every update).

If you don’t want to use a constant force, alternatives would include applying an impulse (can’t remember for sure whether the Unity API exposes this as an option), or simply set the velocity directly (which might be the easiest method). If however you want the rocket to accelerate towards the target over time, you’ll want to use a constantly applied force (perhaps along with an initial non-zero velocity).

Oh sorry for being non specific :frowning: I wanted to make the rocket be fired in a constant force in the direction of the cross hair object that i have attached to my mouse orbit.

So that the rocket is fired from the spawnPoint with a rotation/direction that means it will go towards and through the cross hair.

I just want to know if you can addForce in a set direction like towards the current Vector3 of an object, and how you would find the current Vector3 of an object.

Hope this clears it up a bit, I’m bad at putting things into words :frowning:

If the rocket is already oriented towards the target, just apply a constant force along the forward direction vector.

If that’s not the answer you’re looking for, perhaps you could clarify further. Are you trying to implement a guided projectile of some sort? Or is the rocket just supposed to go straight in the direction it was fired?

well currently i have the rocket firing in the correct direction, but not the correct angle. I would like to be able to have it firing not just forward but also at different angles up and down

say the spawn point is 0,0,0 and the crosshair is at 0,7,20 the rocket is spawned at 0,0,0 I want it to be fired towards the crosshair where now it just goes forwards with

rocket.rigidbody.AddForce(transform.forward *2000);

If there was some code to do something like;

rocket.rigidbody.AddForce(transform.Vector3."crosshair" *2000);

Hm, I guess I’m confused. Earlier you said you already have it working so that the rockets face towards their destination, in which case applying a force along the forward vector should do the trick.

If you don’t already have the rockets oriented correctly though, take a look at the Quaternion.LookRotation() function; it should do what you’re wanting.

Also, if you don’t care about the rotation and just want to apply a force in the appropriate direction, you can do something like this:

Vector3 diff = target.position - transform.position;
diff.Normalize();
rigidbody.AddForce(diff * thrust);

I solved my entire problem with adding one word -.-’ changing AddForce to AddRelativeForce fixed everything, It was using the global not the local z axis I guess so even tho I had rotation and direction working correctly the elevation was global not local or something…

Massive noob mistake thanks for your help tho :slight_smile: