Hi all,
After making a lot of progress today getting a character to move on a plane responding to clicks, I’ve hit a wall when it comes to rotation and direction. I now have the character moving nicely and a smooth rotation using Quaternion.Slerp, but it always ‘turns back to the front’ direction during the move.
My face function:
// Make the character face the direction of movement
void FaceMovementDirection( )
{
Quaternion targetRotation = Quaternion.LookRotation ( targetLocation - thisTransform.position );
targetRotation.z = 0;
targetRotation.x = 0;
//targetRotation.y = 0;
float smooth = 2.0F;
Vector3 tempVector = targetLocation - thisTransform.position;
if (tempVector.magnitude > 0.1)
{
thisTransform.rotation = Quaternion.Slerp (thisTransform.rotation,
targetRotation,
smooth * Time.deltaTime);
}
}
The move function:
// Control the character
void CharacterControl()
{
// Check to see if a new destination has been found
if (state == (int)ControlState.FoundNewDestination )
{
// get a new movement position
RaycastHit hit;
Ray ray = cam.ScreenPointToRay ( Input.mousePosition ); // Get the position
if ( Physics.Raycast (ray, out hit) )
{
// Found a new position, now is it a valid target?
float clickDist = ( transform.position - hit.point ).magnitude;
if (clickDist > minimumDistanceToMove )
{
Debug.Log ("Distance to Target:" + clickDist);
targetLocation = hit.point;
moving = true;
}
}
}
Vector3 moveDirection = Vector3.zero; // declares an empty movement vector
if ( moving )
{
if (character.isGrounded)
{
moveDirection = targetLocation - thisTransform.position;
moveDirection.y = 0;
moveDirection = thisTransform.TransformDirection(moveDirection);
moveDirection *= speed;
}
moveDirection.y -= gravityForce * Time.deltaTime;
character.Move ( moveDirection * Time.deltaTime );
}
FaceMovementDirection();
}
Any and all help would really be appreciated!