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 haystack · Oct 31, 2014 at 12:35 PM · rigidbodyvector3addforce

Can't seem to use AddForce

Just trying to use AddForce to add an upwards force to an instantiated sphere. Must be something I'm leaving out or not coding properly. Here's my code. Note: touchLength is a variable which will have a value between 1 and 250, usually closer to 250.

 void Update () 
 {
     if (Input.touchCount == 1)
     {
         if (Input.GetTouch(0).phase == TouchPhase.Ended)
         {
             //create sphere
             GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             //add a rigidbody
             Rigidbody spheresRigidBody = sphere.AddComponent<Rigidbody>(); 
             //add mass
             spheresRigidBody.mass = 5;
             
             //fling sphere
             sphere.rigidbody.AddForce(new Vector3(0, touchLength, 0));
         }
         
     }
 }
 
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 Wisearn · Oct 31, 2014 at 12:45 PM 0
Share

What are you seeing in unity as a result from this script?

A.k.a what is going differently than you want it to?

avatar image haystack · Oct 31, 2014 at 12:51 PM 0
Share

The ball is created and then simply drops from gravity. No upwards force is being applied to the sphere at all, not even when touchLength == 250.

avatar image sumit9236 · Oct 31, 2014 at 01:05 PM 0
Share

You can check your drag,is$$anonymous$$inematic and static type because all these thing are related to rigidbody.

avatar image haystack · Oct 31, 2014 at 01:12 PM 0
Share

Drag and is$$anonymous$$inematic are not set anywhere, so I would guess they are at their defaults. I'm pretty sure is$$anonymous$$inematic is off as the sphere falls because of gravity. I'm not sure what static type is or how to check that.

2 Replies

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

Answer by Wisearn · Oct 31, 2014 at 01:23 PM

You just need more force, try

 sphere.rigidbody.AddForce(new Vector3(0f, touchLength*50f, 0f));

5 in mass is INSANE, even with 1 in mass you need touchLength*10f to even see any change

so you can also change if you want

 spheresRigidBody.mass = 1f;

or alter the touchLength*50f to higher value than 50f until it's good

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 haystack · Oct 31, 2014 at 01:36 PM 0
Share

That worked. Thanks! Wish I could upvote.

avatar image Wisearn · Oct 31, 2014 at 01:36 PM 0
Share

You can accept it as an answer on the button below the downvote!

and np

Regular thing to try when things arent working as you think it should is to try using extremes (extremely high or extremely low or even negative) for the values.

Harshad$$anonymous$$'s answer here is also quite relevant, that you should try different forcemodes and different ways of applying force, even setting the velocity of the item directly might be more suitable for certain circumstances.

avatar image
1

Answer by HarshadK · Oct 31, 2014 at 01:31 PM

The reason behind your object not being pushed upwards when the force applied is because your AddForce adds regular force (ForceMode.Force) that too in one frame only. This force applied is not enough to push your object upwards since in ForceMode.Force the mass also comes into picture and mass of your object is 5 which makes your applied force insufficient to push your object upwards.

Just to test this add a force with ForceMode.Acceleration which ignores the mass and you'll find that your object is actually pushed upwards a little bit. Or you can even reduce the mass of your object to test this like setting mass to 1.

If you want to throw your object based on some value where you apply force just once (like you are currently doing) you can apply a ForceMode.Impulse. Mind that applying a force equal to 255 with impulse will cause your object just to go way too high. You can actually adjust the value of force as per your need to apply to ForceMode.Impulse.

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 haystack · Oct 31, 2014 at 02:07 PM 0
Share

Thanks, I appreciate it!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Movement Relative to both Camera and XZ Plane 3 Answers

Top Down car/vehicle movement 1 Answer

AddForce to a randomly selected GameObject with a rigidbody 0 Answers

Enemies are moving aroung wildly 1 Answer

Vector3 Parameters and AddForce Not Working Out 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