- Home /
Player Movement & Blend Tree - Can anyone help with my script?
So I have my blend tree setup with idle in center, walking, running for all directions strafe left, right etc. It's a top down game using both analog sticks, the left moves the in the direction and the right rotates them to face the sticks direction. The system walks apart from I can slightly move the left stick to walk as no matter how much force I push on the left stick it always returns -1 or 1 and not 0.5 or -0.5 for walking animations? My script is below does anyone have any idea what I'm doing wrong here? I've hit that coding break wall and smack my head against it hard!
Any help would be massively appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerMovement : MonoBehaviour {
public GameObject model;
public Animator animator;
private Vector2 LeftAxis;
private Vector2 RightAxis;
private Vector3 lastLeftStickInputAxis; // stores the axis input from the left stick between frames
private void Update()
{
/* START LOCOMOTION */
LeftAxis = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
RightAxis = new Vector2(Input.GetAxis("Horizontal2"), Input.GetAxis("Vertical2"));
// Get the axis from the left stick (a Vector2 with the left stick's direction)
var leftStickInputAxis = LeftAxis;
// Get the angle between the the direction the model is facing and the input axis vector
var a = SignedAngle(new Vector3(leftStickInputAxis.x, 0, leftStickInputAxis.y), model.transform.forward);
// Normalize the angle
if (a < 0)
{
a *= -1;
}
else
{
a = 360 - a;
}
// Take into consideration the angle of the camera
//a += Camera.main.transform.eulerAngles.y;
var aRad = Mathf.Deg2Rad*a; // degrees to radians
Debug.Log(leftStickInputAxis);
// If there is some form of input, calculate the new axis relative to the rotation of the model
if (leftStickInputAxis.x != 0 || leftStickInputAxis.y != 0)
{
leftStickInputAxis = new Vector2(Mathf.Sin(aRad), Mathf.Cos(aRad));
}
float xVelocity = 0f, yVelocity = 0f;
float smoothTime = 0.05f;
// Interpolate between the input axis from the last frame and the new input axis we calculated
leftStickInputAxis = new Vector2(Mathf.SmoothDamp(lastLeftStickInputAxis.x, leftStickInputAxis.x, ref xVelocity, smoothTime), Mathf.SmoothDamp(lastLeftStickInputAxis.y, leftStickInputAxis.y, ref yVelocity, smoothTime));
// Update the Animator with our values so that the blend tree updates
animator.SetFloat("VelX", leftStickInputAxis.x);
animator.SetFloat("VelZ", leftStickInputAxis.y);
lastLeftStickInputAxis = leftStickInputAxis;
/* END LOCOMOTION */
/* START ROTATION */
// Get the axis from the right stick (a Vector2 with the right stick's direction)
var rightStickInputAxis = RightAxis;
if (rightStickInputAxis.x != 0 || rightStickInputAxis.y != 0)
{
float angle2 = 0;
if (rightStickInputAxis.x != 0 || rightStickInputAxis.y != 0)
{
angle2 = Mathf.Atan2(rightStickInputAxis.x, rightStickInputAxis.y)*Mathf.Rad2Deg;
if (angle2 < 0)
{
angle2 = 360 + angle2;
}
}
// Calculate the new rotation for the model and apply it
var rotationTo = Quaternion.Euler(0, angle2 + Camera.main.transform.eulerAngles.y, 0);
model.transform.rotation = Quaternion.Slerp(model.transform.rotation, rotationTo, Time.deltaTime*10);
}
/* END ROTATION */
}
private float SignedAngle(Vector3 a, Vector3 b)
{
return Vector3.Angle(a, b) * Mathf.Sign(Vector3.Cross(a, b).y);
}
}
Answer by StalwartList · Oct 26, 2020 at 11:21 AM
Hello this is my first time posting but i also ran into this problem.
the way you fix this is by multiplying your left stick axis by the current left stick velocity. for example.
velocity = Mathf.Abs(LeftAxis.x) + Mathf.Abs(LeftAxis.y);
animator.SetFloat("VelX", leftStickInputAxis.x * velocity);
animator.SetFloat("VelZ", leftStickInputAxis.y * velocity);