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 /
  • Help Room /
avatar image
0
Question by Tankenstein · Dec 27, 2016 at 10:24 PM · rigidbodypositionforcefollowtorque

Question about having a Rigidbody follow another GameObject via forces.

disclaimer: I'm extremely new to Unity (second week)

Hey guys, as an intro to the physics system of Unity, i'm making a sword fighting game. I've got most systems i'm trying to build working, but i'm struggling with something that seems quite basic.

I'm trying to make all of the swordfighting happen via rigidbodies, so blocking and hitting feels real (or at least interesting). The first step of this is to make the "sword" object follow my player's head (first person, head rotates around, camera attached to head). The way i did this at first was very simple, by just nesting the sword as a child of the head, or just moving the transform in code. However, i want it to be possible to move the sword (via an external interaction, like another sword hitting it) at any time. So, i decided to attach a Rigidbody to the sword, and try to make it follow the player using forces.

The system i wrote almost does this, by calculating the difference in position and rotation and applying a force and torque to move towards said position and rotation. I'm using Rigidbody's built-in drag to slow it down. This is the system i'm using right now:

 [RequireComponent(typeof(Rigidbody))]
 public class SwordController : MonoBehaviour {
 
   public Transform followObject;
 
   public float followSpeed = 20000f;
   public float angularFollowSpeed = 20000f;
   public float drag = 10f;
   public float angularDrag = 10f;
 
   private Rigidbody body;
 
   private void Start() {
     body = GetComponent<Rigidbody>();
     body.drag = drag;
     body.angularDrag = angularDrag;
     body.useGravity = false; // hand is holding, no need to use gravity.
   }
 
   private void FixedUpdate() {
     // follow the followObject using force
     // this requires drag on the rigidbody (not to go back and forth like crazy), which is set in Start
     Vector3 difference = followObject.position - transform.position;
     if (difference.sqrMagnitude > float.Epsilon) {
       body.AddForce(difference * followSpeed * Time.deltaTime, ForceMode.Force);
     }
 
     // follow the rotation of followObject using force
     // also requires angular drag on the rigidbody not to go back and forth like crazy.
     Quaternion rotationDifference = (Quaternion.Inverse(transform.rotation) * followObject.rotation);
     Vector3 rotationDifferenceVector = new Vector3(rotationDifference.x, rotationDifference.y, rotationDifference.z);
     if (rotationDifferenceVector.sqrMagnitude > float.Epsilon) {
       body.AddTorque(rotationDifferenceVector * Time.deltaTime * angularFollowSpeed, ForceMode.Force);
     }
   }
 }

However, this doesn't really work. It works for the position, albeit the massive drag and followSpeed numbers i'm using make me feel like i'm doing this in a wrong way. The rotation doesn't work for certain "quarters" of rotation, where it starts doing this weird orbiting thing, also, it's extremely jittery, to an unusable extent.

I would love to hear if i'm approaching this in a very wrong way, or if there's a nice (or not so nice) way to fix my rotation problem.

Thanks!

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
0

Answer by springwater · Dec 28, 2016 at 02:51 PM

There are many ways to solve this I imagine, but bear in mind. It wont just be the sword movement but also forearm strength of the defender that will give the final result. I think you need a spring joint where the sword attaches. Then on collision, make the rigid body for the sword active, that seems to be a good guess. Then apply counter forces representing the control of the forearm.?

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 Tankenstein · Dec 28, 2016 at 08:23 PM 0
Share

$$anonymous$$aking the rigid body active only on collision is a good idea, and i'll try it out. However, applying counter forces representing the control of the forearm is the main problem i'm facing here (this is essentially what the code i wrote originally should be doing). Even if i make it active only on collision, i'll still face the same rotation problem when trying to move the sword back to it's neutral position.

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

99 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

Related Questions

Instant rotation bug when rotating clockwise 0 Answers

Falling Platform 1 Answer

Torque applies to one wheel, but inconsistently to the other wheel 0 Answers

Rocket Landing 1 Answer

Transform.position of object not the same as shown on the inspector 1 Answer


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