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 Taxen0 · May 29, 2014 at 10:55 PM · rigidbodyspawnforceforcemode

How to add a force to a Rigidbody2D?

Hello,

I'm working on my first project and I would like to get some help with using the Rigidbody2D.addForce() command.

This is what i have so far and I would like it to add a force to the bullets actually fly out of the gun instead of dropping down instantly. I am sure I am using the addForce method wrong (last line) so could anyone set me straight?

 using UnityEngine;
 using System.Collections;
 
 public class fireBullets : MonoBehaviour
 {
     public GameObject projectileType; //the prefab that should be spawned.
     
     void Update ( )
     {
         if (Input.GetMouseButtonDown(0)) //when left mousebutton is pressed.
         {
             Fire(); //fire projectile.
         }
     }
     
     void Fire( )
     {
         Transform spawn = transform.Find( "Spawn" ); //find the posotion where the projectile should spawn.
         GameObject projectile = Instantiate( projectileType, spawn.position, spawn.rotation ) as GameObject; //create projectile.
         Vector2 force = new Vector2 (10, 0); //create a force.
         projectile.Rigidbody2D.addForce(force, ForceMode2D.Impulse); //add it to the newly created projectile.
     }    
 }
Comment
Add comment
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

1 Reply

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

Answer by meat5000 · May 29, 2014 at 11:13 PM

Use a Capital A in AddForce. You might also need a small r in the call to the rigidbody (only).

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 Taxen0 · May 30, 2014 at 12:27 AM 0
Share

Thanks, that seems to fix a few compiler errors! I am used to lowerCamelCase from previous program$$anonymous$$g experiences =/

Here are the errors that still show up:

 Assets/Scripts/fireBullets.cs(21,56): error CS0103: The name `Force$$anonymous$$ode2D' does not exist in the current context
 Assets/Scripts/fireBullets.cs(21,40): error CS1501: No overload for method `AddForce' takes `2' arguments

Seems like I add the Force$$anonymous$$ode incorrectly.

avatar image Taxen0 · Jun 06, 2014 at 10:28 PM 0
Share

$$anonymous$$y bad, had more typos

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

22 People are following this question.

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

Related Questions

Difference and uses of rigidbody force modes ? 2 Answers

calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer

How can i set AddRelativeForce relative to how fast/slow im moving my mouse? 2 Answers

Add force which throws back an object 3 Answers

How to get all forces applied to a rigidbody 0 Answers


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