Hello, all. I am a unity 5 user and I can not figure out how to correct my problem. I have a blend tree called blendSpeed that I am trying to control through animator by getting the component only when I call velocity. My character freaks out and I can not figure it out I have tried multiple ways for the past two days passining various vector3 values and floats setting velocity and calling magnitude in the fixed update. I believe my problem is my use of transform.Translate to move my characters horizontal and vertical. I Think I am not understanding the mathematics involved so if anyone can point me in the right direction to correct my problem, it would be greatly appreciative.
public class PlayerInput : MonoBehaviour
{
public float movementSpeed = 10;
public float turningSpeed = 60;
private Rigidbody playerRigidBody;
private Animator anim;
private float horizontal;
private float vertical;
private Vector3 currentVelocity;
private Vector3 previousPosition;
private Vector3 currentPosition;
private Vector3 moveVelocity;
private Vector3 moveInput;
void Start()
{
playerRigidBody = GetComponent<Rigidbody>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
float mouseX = Input.GetAxis("Mouse X") * turningSpeed * Time.deltaTime;
transform.Rotate(0, mouseX, 0);
vertical = Input.GetAxis("Vertical") * movementSpeed * Time.deltaTime;
transform.Translate(0f, 0f, vertical);
horizontal = Input.GetAxis("Horizontal") * movementSpeed * Time.deltaTime;
transform.Translate(horizontal, 0f, 0f);
Animating();
}
void FixedUpdate()
{
if (vertical > 0 || horizontal > 0)
{
playerRigidBody.velocity = (transform.position);
}
}
void Animating()
{
anim.SetFloat("blendSpeed", playerRigidBody.velocity.magnitude);
}
}