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 cubeisbad · Feb 18, 2020 at 03:01 AM · rigidbodyslerprigidbody.moveposition

Preventing rigidbody jitter when MovePosition coroutine ends

I'm making a system possibly best described as telekinesis. The player clicks to pick up the nearest moveable rigidbody, which then slerps to an anchor transform (which is bobbing up and down on a sine wave, so it looks like the rigidbody is floating up and down). This part is working fine. When I left click again, however, the rigidbody is meant to simply drop in place. The problem is, it does this weird jitter thing as if it can't decide whether it should be following the anchor or dropping to the ground.

My code looks sort of like this.

First, check for the correct input:

             if (holding == false && keyDown == false && abilitySwitch.disabling == false)
             {
                 //Right click picks up the closest dragable.
                 if (Input.GetMouseButtonDown(1))
                 {
                     PickupClose();
                 }

...

             else if (holding == true && keyDown == true && abilitySwitch.disabling == false)
             {
                 //Right click while holding executes an indirect attack.
                 if (Input.GetMouseButtonDown(1))
                 {
                     AttackIndirect();
                 }

The pickup function looks like this:

     public void PickupClose()
     {
         dragTarget = ClosestDragable();
         if (ClosestDragable() != null)
         {
             Debug.Log("Pickup close has run.");
             Debug.Log(dragTarget);
 
             holding = true;
             keyDown = true;
 
             dragTarget.GetComponent<Rigidbody>().useGravity = false;
             startCo = true;
         }
         else
         {
             Debug.Log("No dragable object in range.");
         }
     }

startCo is checked in FixedUpdate, and if it is enabled, then the following coroutine is called:

     private IEnumerator MoveTargetToPosition(Vector3 target)
     {
         float t = 0f;
         Vector3 start = dragTarget.transform.position;
 
         while (t <= 1)
         {
             t += Time.fixedDeltaTime / baseSpeed;
             dragTarget.GetComponent<Rigidbody>().MovePosition(Vector3.Slerp(start, target, t));
 
             yield return null;
         }
     }

When the attack function is run:

     public void AttackIndirect()
     {
         Debug.Log("Attack indirectly has run.");
 
         holding = false;
         keyDown = false;
         isHeld = false;
 
         startCo = false;
         dragTarget.GetComponent<Rigidbody>().useGravity = true;
         dragTarget.GetComponent<Rigidbody>().velocity = Vector3.zero;
     }

The anchor's sine wave/position changing transform is handled by a separate script on an empty gameobject:

     void Update()
     {
         Vector3 pos = bobber.transform.position;
         float newY = (Mathf.Sin(Time.time * speed)/4);
         transform.position = new Vector3(pos.x, newY + 2, pos.z);
     }

Here's a video of what the problem looks like.

As you can see, the sphere seems to be trying to fall and follow the target at the same time. When I slowly rotate as I run the attack function, the ball slides from where it should drop to around halfway between that point and where the anchor ends up.

How can I avoid this issue while still making this reasonably fluid?

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

0 Replies

· Add your reply
  • Sort: 

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

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

gameObject can't move and collide after setting it off and setting it on again. 0 Answers

Rigidbody.MovePosition doesn't look smooth 2 Answers

Movement stutter with rigidbody 0 Answers

How can I get a rigid body object to face in the direction my joystick is pointing smoothly? 0 Answers

Ball Rolling is not working 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