hey guys, i have an issue rotating 2d active ragdoll. it’s brokes when rotates multiple times and teleporting up and down. here is code for movement and balancing:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Animator anim;
public Rigidbody2D rb;
public float jumpForce = 10f;
public float playerSpeed = 5f;
public float positionRadius = 0.3f;
public LayerMask ground;
public Transform playerPos;
private bool isOnGround;
private float moveHorizontal;
private Transform hip;
private bool isFlipped = false;
private float lastFlipTime = 0f;
private const float flipDelay = 0.05f;
void Start()
{
hip = transform.Find("Hip");
}
void Update()
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
float playerHipX = hip.position.x;
if ((mousePos.x >= playerHipX && transform.localScale.x > 0) ||
(mousePos.x < playerHipX && transform.localScale.x < 0))
{
if (!isFlipped && Time.time - lastFlipTime >= flipDelay)
{
// Flip character based on mouse position (inverted)
// Save the current position of the player
Vector3 originalPosition = transform.position;
// Change the pivot point to the Hip position
Vector3 hipOffset = transform.position - hip.position;
transform.position = hip.position + hipOffset * -1;
transform.localScale = new Vector3(-transform.localScale.x, 1, 1);
isFlipped = true;
lastFlipTime = Time.time;
}
}
else
{
isFlipped = false;
}
moveHorizontal = Input.GetAxisRaw("Horizontal");
isOnGround = Physics2D.OverlapCircle(playerPos.position, positionRadius, ground);
if (isOnGround)
{
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
if (moveHorizontal != 0)
{
anim.Play(moveHorizontal > 0 ? "WalkBack" : "Walk");
rb.AddForce(Vector2.right * moveHorizontal * playerSpeed, ForceMode2D.Force);
}
else
{
anim.Play("Idle");
}
}
else
{
anim.Play("Idle");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Balance : MonoBehaviour
{
public float targetRotation;
public Rigidbody2D rb;
public float force;
void FixedUpdate()
{
rb.MoveRotation(Mathf.LerpAngle(rb.rotation, targetRotation, force * Time.fixedDeltaTime));
}
}