Question by
loizos-95b · Sep 16, 2020 at 07:58 PM ·
joysticklocomotion
how to control animator controller with 4 floats?
I have been trying to work with a controller and move a character. The issue is that it doesn't read the joystick as joystick but as 4 different buttons. I want to controll a 2D freeform Directonal blend tree to provide 360 locomotion. How to achieve that using 4 different floats?
Here is my code:
public Animator animController;
public CharacterController controller;
public float speed = 5f;
PlayerControls controls;
float up;
float down;
float left;
float right;
private void Awake()
{
controls = new PlayerControls();
//Button A
controls.movement.run.performed += ctx => print("helloooo");
//Joystick
controls.movement.joystick_up.performed += ctx => up = ctx.ReadValue<float>();
controls.movement.joystick_up.canceled += ctx => up = 0f;
controls.movement.joystick_down.performed += ctx => down = ctx.ReadValue<float>();
controls.movement.joystick_down.canceled += ctx => down = 0f;
controls.movement.joystick_left.performed += ctx => left = ctx.ReadValue<float>();
controls.movement.joystick_left.canceled += ctx => left = 0f;
controls.movement.joystick_right.performed += ctx => right = ctx.ReadValue<float>();
controls.movement.joystick_right.canceled += ctx => right = 0f;
}
void Update()
{
Vector3 horizontal = new Vector3(right, 0f, -down) * speed * Time.deltaTime;
Vector3 vertical = new Vector3(-left, 0f, up) * speed * Time.deltaTime;
transform.Translate(horizontal, Space.Self); //Moves the player right and down.
transform.Translate(vertical, Space.Self); //Moves the player left and up
//Control animation Floats.
Vector2 updown = new Vector2(vertical.z, horizontal.z);
Vector2 leftright = new Vector2(vertical.x, horizontal.x);
animController.SetFloat("Horizontal", updown); //ERROR HERE (also when I use the horizontal Vector3)
animController.SetFloat("Vertical", leftright); //ERROR HERE (also when I use the vertical Vector3)
}
void OnEnable()
{
controls.movement.Enable();
}
void OnDisable()
{
controls.movement.Disable();
}
Comment