Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 MrGurbiful · May 06, 2019 at 08:45 PM · physicsrigidbodymousepositionaddtorque

AddTorque from position of mouse/touch?

Hey there,

I like to do a game, which you have an object that is jumping, depending on which place you clicked at it. Now I already have this function:

                     Vector3 Center = this.transform.position;
                     Vector3 forward = Vector3.up;
 
                     Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                     Vector3 forceDirection = (Center - clickedPosition).normalized;
 

but it doesn't look so good without rotating from the direction you clicked. That's the problem. I need help to addTorque that rotates at the opposite direction you clicked. Both left and right (z-axis). I hope everybody will understand...

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
1
Best Answer

Answer by K-Anator · May 06, 2019 at 11:50 PM

Sounds like you need to be working with RelativeTorque or RelativeForce. https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeTorque.html https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html

EDIT: My bad, it's actually https://docs.unity3d.com/ScriptReference/Rigidbody.AddForceAtPosition.html that's going to be helping you... I'll see if I can't whip up a script for you.

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 MrGurbiful · May 07, 2019 at 01:23 PM 0
Share

Tanks for the fast answer $$anonymous$$-ANator :D

I couldn't get this to run:

 void Update()
 {
     if (Input.Get$$anonymous$$ouseButtonDown(0))
     {
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit , 100))
         {
             if (hit.transform.gameObject == pusher)
             {
                 Vector3 Center = this.transform.position;
                 Vector3 forward = Vector3.up;

                 Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 Vector3 forceDirection = (Center - clickedPosition).normalized;

                 rb.AddForceAtPosition(forceDirection * strength, rb.transform.position,
                 Force$$anonymous$$ode.VelocityChange);
             }
         }
     }

Now it's jumping good but still the rotation is zero...

Can you tell me, what is wrong?

avatar image K-Anator MrGurbiful · May 07, 2019 at 04:01 PM 1
Share

Unfortunately I can't quite tell you what's wrong. I spent more time on this last night than I care to admit and never managed to figure out how to make a rigidbody react where I clicked it, lol. Though I did just stumble across this: https://docs.unity3d.com/ScriptReference/RaycastHit-point.html

EDIT: Actually that's a lie, I think I might know why it's not rotating. Right now you're adding force at the RB's position, which is it's anchor point. Ins$$anonymous$$d, we need to apply force where it was hit by the ray, in this case hit.point. And while were at it, there's no need to calculate the forceDirection separately since we already know the force direction, which is the direction of the ray ray.direction. What you will probably need though is a multiplier for the force, so I would suggest adding a pokeForce float as in the example code, then multiply the force direction by it.

avatar image MrGurbiful K-Anator · May 08, 2019 at 12:37 PM 0
Share

Thank you for your help. It didnt work right with ray.direction but I replaced it with the precalculated direction and now it's working.

     void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 100))
             {
                 if (hit.transform.gameObject == pusher)
                 {
                     Vector3 Center = this.transform.position;
 
                     Vector3 clickedPosition = 
                     Camera.main.ScreenToWorldPoint(Input.mousePosition);
                     
                     Vector3 forceDirection = (Center - clickedPosition).normalized;
 
                     rb.AddForceAtPosition(forceDirection * strength, hit.point);
                 }
             }
         }
 

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

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

How to rotate with a Pid controller 0 Answers

Rigidbody Add Torque with Mouse Drag 0 Answers

Giving a ball "english" (side spin) 1 Answer

If a rigidbody is attached, should you always use AddTorque/AddRelative force for rotation/movement? 1 Answer

AddTorque stops working when model is vertical 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