Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 Noxury · Dec 12, 2020 at 09:17 PM · rigidbodychildtorquelocalrotation

Apply torque on child

I have a minigun object parented to my rigidbody character and whenever I start firing, its barrel (child of minigun) should rotate on the local Z axis. I want to use torque since it should smoothly rotate in and out when not firing anymore.

I have attached a rigidbody on the barrel and set all constraints except the Z-rotation. The barrel smoothly rotates on its local Z axis, but when I rotate the character, the barrel stays at the starting global direction (local X,Y angles are char.rot - initial rot, Z is modiied by local torque). Setting the rigidbody to kinematic freezes its local pos/rot, but obviously wont take any torque.


I could use transform.Rotate(Vector3.forward * speed, Space.Self) but therefore I must reimplement torque without using rigidbody.


Is the use case possible just with physics components?

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

Answer by Bunny83 · Dec 13, 2020 at 05:10 AM

I want to use torque since it should smoothly rotate in and out when not firing anymore.

I have attached a rigidbody on the barrel

No, please don't. Rigidbodies are for physics simulations and rigidbodies do not support client space simulations at the moment. Every rigidbody is simulated in worldspace. It would just creates tons of issues you have to work around. Just use your own rotationSpeed variable which you increase / decrease over time whenever you want to spin up / slow down the rotation. Now you can simply apply a local space rotation to the child object based on your rotationSpeed variable. You most likely want to clamp the rotation to some max rotation speed. You can easily achieve all of those by simply using Mathf.MoveTowards

So lets define three variables, targetRotationSpeed, rotationAcceleration and rotationSpeed.

 public float targetRotationSpeed;
 public float rotationAcceleration;
 private float rotationSpeed;

In Update you can simply do

 {
     rotationSpeed = Mathf.MoveTowards(rotationSpeed, targetRotationSpeed, rotationAcceleration * Time.deltaTime);
     barrel.transform.Rotate(0, 0, rotationSpeed * Time.deltaTime, Space.Self);
 }

This will spin the "barrel" object around its z axis based on the current rotationSpeed. You can simply set the targetRotationSpeed to your desired rotation speed and the barrel will slowly spin up based on the "rotationAcceleration". When you want to stop the rotation you can set your targetRotationSpeed to 0 and it will slow down the same way.

Comment
Add comment · Show 3 · 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 Noxury · Dec 15, 2020 at 04:24 PM 0
Share

@Bunny83 Thanks for the right direction to not use rigidbodies. But I notice, that the rotation of the transform flips between negative and positive over time. It slowly accelerates, then deaccelares until 0, then towards the reverse direction. eg. setting target rotationSpeed to 360 and accel to 90.

avatar image Bunny83 Noxury · Dec 15, 2020 at 06:41 PM 0
Share

I'm not sure what you mean and why you would set the rotation speed to 360. It's a speed. So the "angle change per second". The acceleration is the (angle per second) change per second.


Keep in $$anonymous$$d that the rotation speed has nothing to do with a certain orientation / rotation but is only about the change over time. If you want to rotate in the opposite direction you have to use a negative target value. The acceleration should stay positive as it just indicates how fast the speed is changed. $$anonymous$$oveTowards will take care about the increment or decrement depending on the target value.

avatar image Noxury Bunny83 · Dec 15, 2020 at 08:05 PM 0
Share

Ahh never$$anonymous$$d, I wanted reach 1 full cycle per second thats why I set it to 360. The flipping direction was an illusion due to high rotation speed of the object (just like when you look at a heli rotor) and oscillating +-acceleration from my script controlling the accelaration.

avatar image
0

Answer by CmdrZin · Dec 13, 2020 at 04:53 AM

This is the code I use when I pick up a weapon:

             // Attach to Player
             other.transform.SetParent(transform);
             // Orient to Player
             other.transform.localPosition = Vector3.zero;
             // Align to Player
             other.transform.rotation = transform.rotation;
             // Adjust orientation
             other.transform.RotateAround(other.transform.position, other.transform.right, -90.0f);
             // Adjust offset to parent
             other.transform.localPosition += new Vector3(0.3f, 1.8f, 1.6f);
 

The Adjust orientation section should get you close. Other than that, make sure the Ridgidbody Constraints for Freeze Rotation are not checked.

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

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 rigidbody relative to jointed other rigidbody? 2 Answers

Maintain world rotation on parenting to another object 0 Answers

How to rotate camera with offset using torque to look at target 0 Answers

Using rigidbody.AddTorque to have character look at cursor 1 Answer

Rigidbody swipe 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