I'm making a game where the character holds a stick horizontally (think a trapeze artist). The character has a rigidbody and collider, but the stick only has a collider and piggybacks off the rigidbody of the parent character. I'm using DOTween to rotate the character 90 degrees, and I'm having issues with the stick's collision when doing so. If the stick collides with anything while the character does this 90 degree turn, I want to stop the tweening and reverse to the original orientation. Here is the code I'm currently using.
private void StartRotation(string direction)
{
float rotation = -90f;
if (direction.Equals("right"))
rotation = 90f;
originalRotation = transform.rotation.eulerAngles;
isRotating = true;
Vector3 targetRotation = new Vector3(originalRotation.x, originalRotation.y + rotation, originalRotation.z);
// Rotate using DOTween
rotationTweener = transform.DORotate(targetRotation, 0.25f)
.OnUpdate(() =>
{
if (CheckForCollisions(targetRotation))
{
ReverseRotation();
}
})
.OnKill(() => isRotating = false); // OnKill callback is called when the animation is stopped
}
private void ReverseRotation()
{
if (rotationTweener != null)
{
rotationTweener.ChangeEndValue(originalRotation, true);
rotationTweener.OnComplete(() => isRotating = false);
}
}
I've tried several attempts at the CheckForCollisions() function and cannot get it to work correctly. All attempts have led to either the stick ignoring collision all together during the tween, or causing the parented character to move its position to accommodate the stick's collision. Any suggestions or direction would be greatly appreciated.