- Home /
how to I use 2d directional animator with the new input system?
Hello Everyone. I am currently working on using the new input system package and I've made a player character than move rather basic. but I wanted to expand the controls with more animations depending on the direction you move (like strafing left or right). right now the character can move forward and the animations are on a 1d blend type. but wanted to use the 2D blend types to have more variation of animations depending on how you move. this is the class for the animations: public class ThirdPersonAnimation : MonoBehaviour { private Animator animator; private Rigidbody rb; private float maxSpeed = 5f;
// Start is called before the first frame update
void Start()
{
animator = this.GetComponent<Animator>();
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
animator.SetFloat("speed", rb.velocity.magnitude / maxSpeed);
}
}
and here is how I use the input system: public class ThirdPersonController : MonoBehaviour { //input fields private ThirdPersonActionsAsset playerActionsAsset; private InputAction move;
//movement fields
private Rigidbody rb;
[SerializeField]
private float movementForce = 1f;
[SerializeField]
private float jumpForce = 5f;
[SerializeField]
private float maxSpeed = 5f;
private Vector3 forceDirection = Vector3.zero;
[SerializeField]
private Camera playerCamera;
private Animator animator;
private void Awake()
{
rb = this.GetComponent<Rigidbody>();
playerActionsAsset = new ThirdPersonActionsAsset();
animator = this.GetComponent<Animator>();
}
private void OnEnable()
{
playerActionsAsset.Player.Jump.started += DoJump;
move = playerActionsAsset.Player.Move;
playerActionsAsset.Player.Enable();
}
private void OnDisable()
{
playerActionsAsset.Player.Jump.started -= DoJump;
playerActionsAsset.Player.Disable();
}
private void FixedUpdate()
{
forceDirection += move.ReadValue<Vector2>().x * GetCameraRight(playerCamera) * movementForce;
forceDirection += move.ReadValue<Vector2>().y * GetCameraForward(playerCamera) * movementForce;
rb.AddForce(forceDirection, ForceMode.Impulse);
forceDirection = Vector3.zero;
if (rb.velocity.y < 0f)
rb.velocity -= Vector3.down * Physics.gravity.y * Time.fixedDeltaTime;
Vector3 horizontalVelocity = rb.velocity;
horizontalVelocity.y = 0;
if (horizontalVelocity.sqrMagnitude > maxSpeed * maxSpeed)
rb.velocity = horizontalVelocity.normalized * maxSpeed + Vector3.up * rb.velocity.y;
LookAt();
}
private void LookAt()
{
Vector3 direction = rb.velocity;
direction.y = 0f;
if (move.ReadValue<Vector2>().sqrMagnitude > 0.1f && direction.sqrMagnitude > 0.1f)
this.rb.rotation = Quaternion.LookRotation(direction, Vector3.up);
else
rb.angularVelocity = Vector3.zero;
}
private Vector3 GetCameraForward(Camera playerCamera)
{
Vector3 forward = playerCamera.transform.forward;
forward.y = 0;
return forward.normalized;
}
private Vector3 GetCameraRight(Camera playerCamera)
{
Vector3 right = playerCamera.transform.right;
right.y = 0;
return right.normalized;
}
private void DoJump(InputAction.CallbackContext obj)
{
if (IsGrounded())
{
forceDirection += Vector3.up * jumpForce;
}
}
private bool IsGrounded()
{
Ray ray = new Ray(this.transform.position + Vector3.up * 0.25f, Vector3.down);
if (Physics.Raycast(ray, out RaycastHit hit, 0.3f))
return true;
else
return false;
}
if there is need for more info you are welcome to ask.