- Home /
active ragdoll rotation is not working
im making a game with an active ragdoll and im using the cinemachine free look camera for the camera but the moment just doesn't work and idk why, I can move but when I press the button to move forward and rotate my camera the players forward direction doesn't change. Here is the code:
public class CharacterController : MonoBehaviour
{
public float speed = 5f;
public ConfigurableJoint hipJoint;
public Rigidbody hip;
public float groundDistance = 0.4f;
public Animator targetAnimator;
bool walk = false;
public bool isGrounded;
public Transform groundCheck;
public Transform cam;
public float turnSmothTime = 0.1f;
float turSmoothVelocty;
public float jumpForce = 10f;
public LayerMask groundMask;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void FixedUpdate()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(hip.transform.eulerAngles.y, targetAngle, ref turSmoothVelocty, turnSmothTime);
Vector3 moveDir = Quaternion.Euler(0f, angle, 0f) * Vector3.forward;
hipJoint.targetRotation = Quaternion.Euler(0f, angle, 0f);
hip.AddForce(moveDir * speed);
walk = true;
}
else
{
walk = false;
}
if (Input.GetButtonDown("Jump") && isGrounded == true)
{
hip.AddForce(0, jumpForce, 0, ForceMode.VelocityChange);
}
targetAnimator.SetBool("Walk", walk);
}
}
Comment