#Code help: Jitter movement on 'combat' thirdperson controller

1 messages · Page 1 of 1 (latest)

vestal aspen
#

Hi, I have these two functions on a player controller I'm making. They're executed in Update:

  {
      Vector3 inputDir = transform.forward * look.y + transform.right * look.x;
      if (inputDir != Vector3.zero)
      {
          transform.forward = Vector3.Slerp(transform.forward, inputDir.normalized, Time.deltaTime * lookSensitivity);
      }
  }

  private void MoveBasic()
  {
      if(playerRb == null)
      {
          Debug.LogWarning($"ThirdPersonController[MoveBasic]: No coreRb assigned.");
      }

      Vector3 moveDirection = transform.forward * move.y + transform.right * move.x;
      moveDirection.Normalize();

      // Handle speed variation
      float activeBoost = 1.0f;
      if (isBoosting)
      {
          activeBoost = boostPower;
      }
      float speed = baseSpeed * activeBoost;

      playerRb.linearVelocity = new Vector3(moveDirection.x * speed, playerRb.linearVelocity.y, moveDirection.z * speed);

  }```

They're driven by Unity Input Action (joystick/mouse vector2, etc). My problem is there's jitter on the object when both look and movement inputs happen. Any advice? I was following a couple tutorials and ended up with this because their explanations didn't actually work - so this is as far as I've gotten.

The object hierarchy is:
- Player (has the rigidbody)
- - Capsule (has visuals)
- - CamTarget

Where a third person aim cinemachine camera is tracking the CamTarget (Position control is Third Person Follow, and  no Rotation Control).
dapper hound
#

You are most likely running into changing world positions on two different spots for two different gameobjects. Could that be the case? If you are moving the world position of a child object while its parent is moving too, it can result to jitter, because the child always tries to follow its parent at the same time

vestal aspen
#

Well updated it (only looks at the rigidbody, no child target) and still jittery:

private void Update()
{
    move = playerInput.actions["Move"].ReadValue<Vector2>();
    look = playerInput.actions["Look"].ReadValue<Vector2>();

    if(move != Vector2.zero)
    {
        MoveBasic();
    }
    if(look != Vector2.zero)
    {
        LookBasic();
    }
}

private void LookBasic()
{
    Vector3 inputDir = transform.forward * look.y + transform.right * look.x;
    if (inputDir != Vector3.zero)
    {
        transform.forward = Vector3.Slerp(transform.forward, inputDir.normalized, Time.deltaTime * lookSensitivity);
    }
}

private void MoveBasic()
{
    if(playerRb == null)
    {
        Debug.LogWarning($"ThirdPersonController[MoveBasic]: No coreRb assigned.");
    }

    Vector3 moveDirection = transform.forward * move.y + transform.right * move.x;
    moveDirection.Normalize();

    // Handle speed variation
    float activeBoost = 1.0f;
    if (isBoosting)
    {
        activeBoost = boostPower;
    }
    float speed = baseSpeed * activeBoost;

   playerRb.AddForce(moveDirection * speed, ForceMode.VelocityChange);

}```

I'm also noticing if I add a pulse mechanic, when doing something like `playerRb.AddForce(moveDirection * pulsePower, ForceMode.Impulse);` on a button press, the player's visual does the pulse but snaps back to the trajectory where the WASD/stick would have moved the player