- Home /
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;
direction.Normalize();
Quaternion rotation = Quaternion.Euler( 0, 0, Mathf.Atan2 ( direction.y, direction.x ) * Mathf.Rad2Deg );
GameObject bullet = (GameObject)Instantiate (ammo, myPos, rotation);
}
}
Ooops, sorry forgot to add, line 24 looks:
rb.Addforce (direction * speed);
First thing to do I$$anonymous$$O is to add logging to the Update function, including the values of the various variables you create in there, so you know if/when that code is running, and what it's actually doing.
Obvious questions it'd answer include "is it not doing anything or is it applying a force of zero?". If it's the latter then "what's the value of speed
?" and "how does target
differ from mypos
?"
Something else I just noticed though... don't you want the force to be applied to the bullet's rigidbody? What is the rb
variable set to?
How do I refer to the projectile's rigidbody for the .Addforce ? I lost it since Unity 5 changed this around.
Answer by MichaelJT · May 30, 2015 at 03:48 PM
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.
Answer by Saad_Khan · May 30, 2015 at 07:07 PM
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.
Answer by Nabeel Akhtar · May 30, 2015 at 07:06 PM
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 :)
Answer by Sethhalocat · May 30, 2015 at 03:29 PM
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.
Hey Seth, I greatly appreciate your ideas, could you perhaps set a simple example up to see how it would work out ? It's not exactly what I had I $$anonymous$$d but it could add some flavour to the game ins$$anonymous$$d.
Sure, i use data for internet but i will set it up with a download by today or tomorrow. Until then start working on maps and test them out when the assets are ready that way you dont have to wait.
Your answer
Follow this Question
Related Questions
Enemy shoot at player in 2D game 3 Answers
Uneven speed in 2d movement script 2 Answers
How to make Enemy shoot at Player Top Down 1 Answer
How do I get my character to shoot a projectile in the direction of the cursor? 1 Answer
Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer