Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Karl_Delacroix · May 30, 2015 at 11:37 AM · 2daddforceshooting

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);
         
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Karl_Delacroix · May 30, 2015 at 12:05 PM 0
Share

Ooops, sorry forgot to add, line 24 looks:

rb.Addforce (direction * speed);

avatar image Bonfire-Boy · May 30, 2015 at 12:12 PM 1
Share

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?"

avatar image Bonfire-Boy · May 30, 2015 at 12:17 PM 1
Share

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?

avatar image Karl_Delacroix · May 30, 2015 at 03:40 PM 0
Share

How do I refer to the projectile's rigidbody for the .Addforce ? I lost it since Unity 5 changed this around.

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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.


screen-shot-2015-05-30-at-82402-pm.png (10.0 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Karl_Delacroix · May 30, 2015 at 03:42 PM 0
Share

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.

avatar image Sethhalocat · May 30, 2015 at 04:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges