• 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 Yons · Mar 03, 2011 at 10:09 PM · physicsaddforcecannonball

AddForce has no effect on my cannonball.

Hey folks, first post so if something is wrong please let me know?

I am creating an augmented reality game with two cannons shooting at each other, I have a script attached to the barrel of my cannon to instantiate a cannonball to be fired, when a button on the mobile device is pressed.

My problem is that the cannonball appears and simply falls to the ground under the force of gravity, I can fire(spawn) lots and they react with one another but there is no movement in any direction other than the vertical axis.

My code is as follows

var cannonBallPrefab : Transform;

function Update () { if(Input.GetKeyDown(KeyCode.Menu)) { var cannonBall = Instantiate(cannonBallPrefab, transform.position, Quaternion.identity) as GameObject;

     cannonBall.rigidbody.AddForce(Vector3.right * 40000);
 }

}

I have read other peoples attempts to solve this problem but none seem to work, it is rigidbody, not kinematic, I have tried excessively large and small forces, large and small masses, there are no mesh colliders other than that of the cannonball itself.

I really dont know what to do

any help would be amazing

Thanks in advance

Euan Hislop

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

3 Replies

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

Answer by Chris D · Mar 03, 2011 at 10:44 PM

I've had luck with my code below. It's got a few extra items in there, but successfully spawns at the cannonballSpawn transform and applies force to the instanced ball, relative to the spawn's position and rotation.

var cannonballSpawn : Transform; var cannonballPrefab : GameObject; var cannonForce : int;

function Update() { if(Input.GetButtonUp("Fire1")) { var projectile = Instantiate(cannonballPrefab, cannonballSpawn.position, Quaternion.identity); projectile.rigidbody.AddRelativeForce(transform.up* cannonForce);//cannon's axis } }

edit:

on taking a look at our codes, the only real difference I see is straight up Instantiating my prefab as a GameObject as opposed to a transform. Maybe there's something happening there?

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 Yons · Mar 03, 2011 at 11:14 PM 0
Share

Genius! Thank you for the help. I am guessing this line 'var cannonBallPrefab : Transform;' is where the problem was co$$anonymous$$g from, although I looked at tutorials that used it, this works! Thank you so much!

avatar image Chris D · Apr 29, 2011 at 05:01 PM 0
Share

No problem, glad to help. Co$$anonymous$$g back and looking at this now, I think I'm making the mistake of adding force in the Update as opposed to FixedUpdate...oh well.

avatar image
3

Answer by tertle · Mar 03, 2011 at 10:19 PM

The default ForceMode of AddForce is ForceMode.Force

ForceMode.Force is intended to be put on an object over various frames.

GetKeyDown will only run for a single frame.

You should look at changing the ForceMode. In this case an Impulse force might work as intended for a realistic cannon effect.

cannonBall.rigidbody.AddForce(Vector3.right* 40000, ForceMode.Impulse);

Note with you applying such a high force, Vector3.right* 40000, your cannon ball is likely to instantly be shot 1000km away.

Comment
Add comment · Show 3 · 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 Yons · Mar 03, 2011 at 10:28 PM 0
Share

Wow, thanks for the speedy comment but unfortunately this did not work.

I tried using Force$$anonymous$$ode.Impulse but there was no effect again, when you say

"Force$$anonymous$$ode.Force is intended to be put on an object over various frames."

do you mean kind of like using the 'FixedUpdate()' function, cause I tried this as well to no effect! I maybe should not have posted using such a high value that was just for testing as some other users had solved there problems by using a higher force.

Thanks

Euan Hislop

avatar image tertle · Mar 03, 2011 at 10:44 PM 0
Share

Ideally you should do all rigidbody stuff (adding forces etc) in FixedUpdate. $$anonymous$$ay be a silly question, but does your cannonBallPrefab actually have a rigidbody attached to it? Finally

avatar image Yons · Mar 03, 2011 at 10:50 PM 0
Share

I tried using FixedUpdate, but once again it did not work, and it was creating two cannonballs sometimes. The cannonBallPrefab does have a rigidbody attached to it, I am very confused, I have tried all the solutions that worked for other people lol Thanks Euan Hislop

avatar image
0

Answer by efge · Mar 03, 2011 at 10:26 PM

If you only want to access the Rigidbody of the instantiated object you could do this.
Define a Rigidbody and set it's velocity along z-axis:

var cannonBall : Rigidbody =  Instantiate(cannonBallPrefab, transform.position, Quaternion.identity);
cannonBall.velocity = transform.TransformDirection (Vector3.forward * factor);
Comment
Add comment · Show 1 · 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 Yons · Mar 03, 2011 at 10:42 PM 0
Share

Hey thanks for the reply

But this also did not work.

I will keep on plugging away though

Thanks

Euan Hislop

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

addforce seems inconsistant 4 Answers

Get result (force & torque) of AddForceAtPosition? 2 Answers

Why is force only being added in the same direction? 1 Answer

How do I add force To rigidbody based On Character's Walking direction? 0 Answers

Can't Get gravity function to work 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