- Home /
Having trouble inserting character into existing third person character controller - How can I script it so that I can move in directions?
Im trying to put my modeled character into the 3rd person controller + Fly. character controller.
My model has stationary looping animations for walk and run, as in only the motion is animated but not the movement in world space itself so when I replaced the movement animations in the controller, my model does not move. I then tried to find out how to fix this and found the scripting root motion page and it works in that I can now move my character but it only moves in a straight line no matter what direction I input.
If anyone can help me find out how to script the movement so that I can move in all directions not just straight I would greatly appreciate it, because I have tried and couldn't make it work.
This is the relevant parts of the 3rd person movement script.
// Deal with the basic player movement
void MovementManagement(float horizontal, float vertical)
{
// On ground, obey gravity.
if (behaviourManager.IsGrounded())
behaviourManager.GetRigidBody.useGravity = true;
// Call function that deals with player orientation.
Rotating(horizontal, vertical);
// Set proper speed.
Vector2 dir = new Vector2(horizontal, vertical);
speed = Vector2.ClampMagnitude(dir, 1f).magnitude;
// This is for PC only, gamepads control speed via analog stick.
speedSeeker += Input.GetAxis("Mouse ScrollWheel");
speedSeeker = Mathf.Clamp(speedSeeker, walkSpeed, runSpeed);
speed *= speedSeeker;
if (behaviourManager.isSprinting())
{
speed = sprintSpeed;
}
behaviourManager.GetAnim.SetFloat(speedFloat, speed, speedDampTime, Time.deltaTime);
}
// Rotate the player to match correct orientation, according to camera and key pressed.
Vector3 Rotating(float horizontal, float vertical)
{
// Get camera forward direction, without vertical component.
Vector3 forward = behaviourManager.playerCamera.TransformDirection(Vector3.forward);
// Player is moving on ground, Y component of camera facing is not relevant.
forward.y = 0.0f;
forward = forward.normalized;
// Calculate target direction based on camera forward and direction key.
Vector3 right = new Vector3(forward.z, 0, -forward.x);
Vector3 targetDirection;
targetDirection = forward * vertical + right * horizontal;
// Lerp current direction to calculated target direction.
if((behaviourManager.IsMoving() && targetDirection != Vector3.zero))
{
Quaternion targetRotation = Quaternion.LookRotation (targetDirection);
Quaternion newRotation = Quaternion.Slerp(behaviourManager.GetRigidBody.rotation, targetRotation, behaviourManager.turnSmoothing);
behaviourManager.GetRigidBody.MoveRotation (newRotation);
behaviourManager.SetLastDirection(targetDirection);
}
// If idle, Ignore current camera facing and consider last moving direction.
if(!(Mathf.Abs(horizontal) > 0.9 || Mathf.Abs(vertical) > 0.9))
{
behaviourManager.Repositioning();
}
return targetDirection;
}
and this is the root motion script that I inserted in
void OnAnimatorMove()
{
Animator animator = GetComponent<Animator>();
if (animator)
{
Vector3 newPosition = transform.position;
newPosition.z += animator.GetFloat("Runspeed") * Time.deltaTime;
transform.position = newPosition;
}
}
Both of those in conjunction resulted in my character only being able to move forward in a straight line no matter if I pressed forward, back, left, or right. Thanks in advance.
Answer by IronKnigh · Aug 20, 2018 at 04:02 AM
Hello there, I recommend you check out my post which should answer your question. There are two parts to my answer and the first part should apply to your question.
Your answer
Follow this Question
Related Questions
How to rotate an object with a script after an animation has rotated it? 1 Answer
Unable to play animation continuously? 1 Answer
loop an animation if the button witch activates it is still pressed 0 Answers
going back to the default state in animator 1 Answer
The 'correct' way to deal with animations in a grid-based game? 1 Answer