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 18, 2019 at 04:13 PM · rigidbodyaddforcedirection

AddForce on Object at Direction of Click

Hi there all, I'm currently struggling to AddForce to an object, the object is a Domino and what i am attempting to achieve, that when i click on either 2 of the main faces (back and front) the domino will fall in the corresponding click direction, i do not however wish for the user to be able to 'tip' the domino sideways, only backward and forward, in relation to whatever the dominos rotation may be.

This piece of code is what I started with, but it would only push the domino in one specified 'world' direction, rotation of the domino made no difference to the direction of force it would always push 'North'.

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             if (hitInfo.collider.CompareTag("Domino"))
             {
                 hitInfo.collider.GetComponent<Rigidbody>().AddForce(transform.forward * forceAmount);
             }
         }

This piece of code is my current piece where I am struggling, the result with this code is that the domino 'hops' toward the camera when clicked and not at the direction that the mouse clicked.

         var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         var mouseDir = mousePos - gameObject.transform.position;
         mouseDir.z = 0.0f;
         mouseDir = mouseDir.normalized;
 
 
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             if (hitInfo.collider.CompareTag("Domino"))
             {
                 hitInfo.collider.GetComponent<Rigidbody>().AddForce(mouseDir * forceAmount);
             }
         }
 
     }

Below are some GIFs, visualizing what is happening in-game.

Old Code: Old Code

New Code: New Code

Main Point: How can i make it so if i click the face of the domino, it will fall onto the back side and clicking the back makes it fall to its face, no matter the rotation of the domino.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kolboch · Jan 18, 2019 at 04:36 PM

You have to use the normal of the object surface detected by raycast. Then you would have to reflect that vector and this will be your force you are willing to apply. Useful docs: https://docs.unity3d.com/ScriptReference/RaycastHit-normal.html

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
0

Answer by xxmariofer · Jan 18, 2019 at 04:37 PM

the easiest way of fixing this is to have 2 childs colliders front face and behind with 2 tags and compare the tag and use your old code but depending on the tag assigned force in Vector3.right and left.

Comment
Add comment · Show 4 · 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 FuryFight3r · Jan 18, 2019 at 04:57 PM 0
Share

this would be great, yet, if i rotated the do$$anonymous$$o, it will still be pushing it 'Left' relative to world space, not the objects local position

avatar image xxmariofer FuryFight3r · Jan 18, 2019 at 07:04 PM 0
Share

i think you confused Vector3.forward (0,0,1) always the same and transform.forward that is the forward vector of the object and changes if you rotate it.

avatar image FuryFight3r xxmariofer · Jan 18, 2019 at 08:21 PM 0
Share

With this code:

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         RaycastHit hitInfo;
         if (Physics.Raycast(ray, out hitInfo))
         {
             if (hitInfo.collider.CompareTag("Do$$anonymous$$o"))
             {
                 hitInfo.collider.GetComponent<Rigidbody>().AddForce(transform.forward * forceAmount);
             }
         }

I Get this Result: alt text

Is this what you mean?

Show more comments

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

134 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

Related Questions

Rigidbody.AddTorque/AddForce Question. 1 Answer

How can I convert velocity/direction to Force? 3 Answers

Directional force for Rigidbody's 'AddForce' 2 Answers

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

throw object at specific direction or angle 2 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