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
1
Question by Atikalaker · Dec 11, 2017 at 09:42 AM · rotationphysicsrigidbodyspin

How can I rotate RigidBody and not violate physics?

Hi. I'm trying to create a game that we have a falling ball and an spinning object that spin based on user mouse position. user should rotate the object and hit the ball on time to throw the ball into sky.

The problem is that if user move his finger too fast. the game object moves too fast and goes past through the ball. This is my code so far:

 void Update () {
             if (Input.GetMouseButtonDown(0)) {
                 startPos = Input.mousePosition.x;
                 startRot = obj.transform.localEulerAngles;
                 lastDist = 0;
             }
     
             if (Input.GetMouseButton(0)) {
                 float dist = Input.mousePosition.x - startPos;
     
                 obj.MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
     
                 for (int i = 0; i < obj.transform.childCount; i++)
                     obj.transform.GetChild(i).GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(startRot + new Vector3(0, 0, -dist)));
             }
     }


How can I achieve this behavior for my game. The user should be able to spin the game object as fast as possible.

Thanks in advance.

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

Answer by tcjulian · Dec 11, 2017 at 09:53 AM

To rotate a rigidbody without violating physics, you'll want to use Torque, which is the rotational equivalent of Force (see https://en.wikipedia.org/wiki/Torque).


In Unity, you should handle any continuous physics adjustments in FixedUpdate instead of Update. To add Torque to a`Rigidbody`, use https://docs.unity3d.com/ScriptReference/Rigidbody.AddTorque.html or https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeTorque.html .

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 Atikalaker · Dec 11, 2017 at 10:02 AM 0
Share

thanks for you answer. This AddTorque method not working with mouse position. It is really important that user can rotate the rigid body with dragging mouse over the screen. How can i do that?

avatar image tcjulian Atikalaker · Dec 11, 2017 at 10:08 AM 0
Share

You can base the torque on the distance and direction of the mouse from the object, or on the angle between the object's facing vector and the vector from the object to the mouse. You'll probably want to project the mouse position into world space first with https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html .

avatar image Atikalaker tcjulian · Dec 11, 2017 at 10:15 AM 0
Share

In this case. if the user drag the mouse for example for 100 pixels and stop. It will continue rotating and it is not the desired behavior for the drag method.

Show more comments
avatar image
0

Answer by othorn · Feb 21, 2020 at 09:38 AM

Use AddRelativeTorque() method. This is an example that rotates an object by adding the force. Unity's physics engine takes care of the rest, no need to run anything in Update().

 // In your Start() method
 Rigidbody rigidbody = GetComponent<Rigidbody>();
 
 
 //Add this in your method that is called once
 // Set a random rotation, because why not
 Vector3 randomRotation = new Vector3(Random.Range(30f, 60f), 0, 0);
 // Apply the rotation to the rigid body
 rigidbody.AddRelativeTorque(randomRotation, ForceMode.Impulse);
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 Fortochka · Jun 04, 2021 at 06:37 PM

How do I set the z value for an object instead of giving the momentum to rotate using RigidBody2D physics? In the sense that angularVelocity, AddTorque sets the pulse, and not the angle to be rotated. If you use rotation, MoveRotation or transform.rotation.z, then the player crashing into an obstacle will not rotate, adjusting his position under the wall. And if I don't set the rotation, but just make the player go into the wall at a slight angle, then he will just be able to turn himself and adjust to the wall. Help please, of course, if what I want is possible at all

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

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

155 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

Related Questions

How to make a dynamic bow shot and the arrow to curve and turn correctly when shot? 0 Answers

transform.Rotate and Rigidbody.MovePosition 1 Answer

How to limit the motion direction of a GameObject? 1 Answer

how to CORRECTLY position and rotate a gameobject in unity 2 Answers

Rotate using physics 6 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