Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 FuryFight3r · Jan 20, 2019 at 04:15 AM · rotationrigidbodyaddforceforce

Make object tip over

Hi all, wondering how i can make an object standing on its end IE. A Domino fall 'FORWARD' to the objects space and rotation NOT world space..

AddForce does not work as expected and only tips the object according to world space. so if i rotate the object 90 degrees it will not longer fall forward, it will now fall sideways....

my current code is useless and nothing as to what i need...

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
                 if (hitInfo.collider.CompareTag("DominoFrontFace"))
                 {
                     hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(transform.position += Vector3.right * forceAmount);
                 }
                 if (hitInfo.collider.CompareTag("DominoBackFace"))
                 {
                     hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(transform.position += Vector3.left * forceAmount);
                 }
         }

with this current code as well as pushing the objects in a direction that makes no sense to the current objects rotation, it also always multiplies the force added, meaning each and every time i click on a domino to make it tip over the force gets stronger and stronger and starts throwing the dominos rather than tipping them over.

Video showing what happens, as people are unaware that adding force only goes in one direction according to world space, no matter the rotation of the domino. Youtube Video

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 DCordoba · Jan 20, 2019 at 12:31 PM 0
Share

I don't know what local space do you mean, this comment is refering to the local space of the object where the script is attached

please try using transform.right (right in local space) ins$$anonymous$$d the vector3.right (world space) and also if u use += you are changing the transform.position manually and also adding force... so on line 8 replace

 hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(transform.position += Vector3.right * forceAmount);

by

 hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(transform.position + transform.right * forceAmount);

and also replace line 12 by

 hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(transform.position - transform.right * forceAmount);

avatar image DCordoba · Jan 20, 2019 at 12:45 PM 0
Share

I don't know what local space do you mean, this comment is refering to add force on the click direction, without using any local space

you can also addforce making a impulse to the direction to you see from the camera, I think this isnt the result you want to archieve, because the camera may not be pointing directly at target and you still want a "right" impulse

so again on line 8 replace by

 hitInfo.collider.GetComponentInParent().AddForce(hitInfo.point + ray.direction * forceAmount);

on line 12 replace by the same code, you dont need know how is the piece oriented here, just its position relative to the camera

avatar image DCordoba · Jan 20, 2019 at 01:09 PM 1
Share

I don't know what local space do you mean, this comment is refering to the local space of the transform piece, so the parent of the collider u point

I think this is the result you want to archieve, cause check the left or right to the piece without care of camera orientation, just if you are pointing the correct face

so, again replace line 8 by

 hitInfo.collider.GetComponentInParent().AddForce(hitInfo.collider.position + hitInfo.collider.parent.right * forceAmount);

and line 12 by

 hitInfo.collider.GetComponentInParent().AddForce(hitInfo.collider.position - hitInfo.collider.parent.right * forceAmount);

last some more (cosmetic) modifications

to evade problems with mistake + with += (the problem that increases force added each time) use specific functions, on this case is AddForceAtPosition

u use the code hitInfo.collider.GetComponentInParent<Rigidbody>() because its almost impossible u click two sides at the same time, doesnt cause computation problems, bu just to not repeat code can create a

 Rigidbody piece =  hitInfo.collider.GetComponentInParent<Rigidbody>();

and use it inside the ifs

avatar image FuryFight3r DCordoba · Jan 21, 2019 at 01:06 AM 0
Share

This worked perfectly, thank you so much, i did make a prior post about this but seemed to be leading people in the wrong direction, I think that's where i got really confused.

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             if (hitInfo.collider.CompareTag("Do$$anonymous$$oFrontFace"))
             {
                 hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(hitInfo.collider.transform.position - hitInfo.collider.transform.parent.forward * forceAmount);
             }
             if (hitInfo.collider.CompareTag("Do$$anonymous$$oBackFace"))
             {
                 hitInfo.collider.GetComponentInParent<Rigidbody>().AddForce(hitInfo.collider.transform.position + hitInfo.collider.transform.parent.forward * forceAmount);
             }
         }


1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by unity_DKqnJkGo8MOKYQ · Jan 20, 2019 at 06:17 PM

AddForce( pos + dir ) makes no sense. AddForce takes a direction, not a position.

The call you want does use world coordinates, but lets you define where on the rigidbody to apply the force: AddForceAtPosition( force, position ).

To always have the domino fall forward in localspace, you can just use the domino's local transformation: body.AddForceAtPosition( body.rotation * Vector3.forward, hitinfo.point );

[1]: https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html

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 DCordoba · Jan 20, 2019 at 07:16 PM 1
Share

or just keep using raycast info and apply on point where the ray collide

 body.AddForceAtPosition(ray.direction, hitInfo.point);

he have to add a collider (cube probably) at the piece gameobject (the parent of the faces) and delete the faces if isn't useful anymore,

also physics, correct me if I'am wrong but the angular force is (approximated by) the vector to the force ( Vector3.right * forceAmount ) plus the vector distance to the relative center... relative to te point of refecence however I still recommed use the AddForceAtPosition

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

160 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddForce not influenced by rotation 1 Answer

add force to object that has 2 different rigid bodies 0 Answers

Add Force without Rotation 2 Answers

Ball Rotating Help 0 Answers

How to rotate object about 2 axes (vertical y and depth z) using configurable joint. 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