- Home /
3d Character rotates in an arc rather than on the spot [video attached]
I've been stuck on this problem for a while and haven't found anyone having similar difficulties. Basically I have a 3rd person character that I am trying to get to move and rotate at the same time. My character moves and rotates to begin with OK, but eventually the character starts to rotate in huge arcs which you can see in the attached video. Any help on this would be appreciated as I've spent too many hours now without getting anywhere.
Here is the video
and here is my code
public float speed = 2.0F;
public float jumpSpeed = 1.0F;
public float gravity = 50.0F;
public float turnSmoothing = 15f;
public float lookSpeed = 100;
public Vector3 moveDirection;
public float moveHorizontal;
public float moveVertical;
public int grounded = 1;
public Transform head;
public Transform groundcheck;
float GroundDistance = 0.5f;
bool IsGrounded()
{
return Physics.Raycast(groundcheck.position, -Vector3.up, GroundDistance);
}
void Update()
{
moveHorizontal = Input.GetAxisRaw("Horizontal");
moveVertical = Input.GetAxisRaw("Vertical");
moveDirection = new Vector3(moveHorizontal, 0, moveVertical);
moveDirection = Camera.main.transform.TransformDirection(moveDirection);
ConstantForce headcf = head.GetComponent<ConstantForce>();
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
if (Input.GetButton("Jump"))
{
if (IsGrounded() == true)
{
moveDirection.y = jumpSpeed;
headcf.force = new Vector3(0, 180, 0);
print("Ground");
grounded = 1;
}
else
{
headcf.force = new Vector3(0, 60, 0);
print("Not Ground");
grounded = 0;
}
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
if (moveHorizontal != 0f || moveVertical != 0f)
{
Rotating(moveHorizontal, moveVertical);
}
}
void Rotating(float mh, float mv)
{
Vector3 targetDirection = new Vector3(mh, 0f, mv);
Quaternion targetRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
moveDirection.y = 0f;
Quaternion newRotation = Quaternion.Lerp(transform.rotation, targetRotation, turnSmoothing * Time.deltaTime);
transform.rotation = newRotation;
}
If the character is a child of another object make sure it's local position is set to 0,0,0. Also make sure the models pivot point is centered.
Why does the Rotating method take movement values as parameters?!?
Answer by steveyj91 · Dec 09, 2016 at 05:08 PM
Thanks @DoubleIsLoveDoubleIsLife , I've just looked at that and the local position of the metarig is set to 0,0,0. I imported the model from blender so could a pivot point be off from my imported model? I'll go back and check.
@Cherno Yeah that doesn't need to happen anymore as they were used in a previous version.