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?
Your answer
Follow this Question
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