I have a blend tree with couple animations that assign movement forward/backward and player rotation. Than I need to add strafe animations. Here the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rpgMovementController : MonoBehaviour
{
[Header("Movement")]
public float currentMovementSpeed = 2.5f;
public float walkSpeed = 2.5f;
public float runingSpeed = 6.5f;
public float rotationSpeed = 90f;
[Header("Jumping")]
public float gravityForce = -9.8f;
public float jumpForce = 5f;
[Header("Containers")]
[SerializeField] private CharacterController playerController;
[SerializeField] private Animator playerAnimator;
[SerializeField] private Vector3 moveVelocity;
[SerializeField] private Vector3 turnVelocity;
[SerializeField] private Vector3 strafeVelocity;
private void Awake()
{
playerController = GetComponent<CharacterController>();
playerAnimator = GetComponentInChildren<Animator>();
}
private void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
float strf = Input.GetAxis("Strafe");
if (z < 0)
{
currentMovementSpeed = walkSpeed;
} else
{
currentMovementSpeed = runingSpeed;
}
if (playerController.isGrounded)
{
moveVelocity = transform.forward * currentMovementSpeed * z;
strafeVelocity = transform.right * currentMovementSpeed * strf;
moveVelocity.y = -2f;
playerAnimator.SetFloat("Forward", z);
playerAnimator.SetFloat("Turn", x);
if (Input.GetButtonDown("Jump"))
{
moveVelocity.y = jumpForce;
}
}
if (playerController.isGrounded)
{
playerAnimator.SetBool("Jump", false);
}
else
{
playerAnimator.SetBool("Jump", true);
}
turnVelocity = transform.up * rotationSpeed * x;
moveVelocity.y += gravityForce * Time.deltaTime;
playerController.Move(moveVelocity * Time.deltaTime + strafeVelocity * Time.deltaTime);
transform.Rotate(turnVelocity * Time.deltaTime);
}
}
