My shooting script doesn't add force to the projectile

Hi there,

I’m a bit of a newb at unity, I’m working on a topdown 2d shooter, and I want my character to shoot rocks towards the mouse pointer. With my current code the rocks appear but keep still in the air. Could someone, please, give it a look ? Both the player prefab and the bullets have rigidbody2d and collider2d components applied. Thanks!

using UnityEngine;
using System.Collections;

public class PlayerShooting : MonoBehaviour {

	public GameObject ammo;
	public Rigidbody2D rb; 
	public float speed;
	// Use this for initialization
	void Start () {
	// Update is called once per frame
	void Update () {
		if (Input.GetMouseButtonDown(0)) {
			Vector2 target = Camera.main.ScreenToWorldPoint( new Vector2(Input.mousePosition.x,  Input.mousePosition.y) );
			Vector2 myPos = new Vector2(transform.position.x,transform.position.y);
			Vector2 direction = target - myPos;
			Quaternion rotation = Quaternion.Euler( 0, 0, Mathf.Atan2 ( direction.y, direction.x ) * Mathf.Rad2Deg );
			GameObject bullet = (GameObject)Instantiate (ammo, myPos, rotation);

I think in this case your "public Rigidbody2d "(rb) is not affecting the bullet because the rigidbody first needs to be added to the bullet game object.

Please replace the following with your “rb.Addforce (direction * speed);” line

Now your bullet has a rigid body component attached to it.

This should solve your problem

P.S …The “speed” in this case should be high enough try >=200 to counter the effect of gravity , which you can either choose to turn off, or change the gravity scale.

Make sure you attaching the right righbody2D to script in Inspector. In your case, it a bullet rigdbody2D. To which force will be applied.

The other way is get Rigdbody2D through script. Like this

bullet.GetComponent().addForce(direction * speed);

Hope this works for you :slight_smile:

I would add a animation that makes it go a certain distance and keep it going further and on a slope. If ur looking for something more realistic then you can add a animation on the hand and give the rock gravity. Then leave it to unity physics to see how far it goes. It would also alow you to bend the rules a bit by changing your rocks gravity. All you need is to spawn a rock and hand on click and give the hand a animation that turns quickly and dependent on where your mouse is, the further it goes. If you would like i can make assets to give you a example

If you want you can give me a thumbs up and vote this as a corrects answere, it would help a lot. Reply if it dosnt work, i all ways check my recent answeres.

I think you’ll need to get a reference to the rigidbody after the prefab is instantiated.

In other words:

GameObject bullet = Instantiate (prefab, position, rotation) as GameObject;
rb = bullet.GetComponent<Rigidbody>();
rb.AddForce (direction * speed);

The issue with the way you’re doing it now is that you’re declaring a rigidbody but never actually assigning it.

I personally find a better way of doing this is putting the acceleration for the projectile into a script attached to the prefab.