I have a character using the mechanim 1D Blend Tree blending from idle to walk to run. I’m using a float parameter called “speed” to control when the animation blend, but the don’t know how to control that in code
anim.SetFloat (“Speed”, ???)
Any direction would be great. Here is my code.
public class PlayerController : MonoBehaviour
{
// Use this for initialization
public float velocity = 5.0f;
public float turnSpeed = 10.0f;
public float jumpForce = 10.0f;
public Vector2 input;
public Vector3 nGravity;
float angle;
private bool isMoving;
public bool ishurt;
public bool canHurt;
public bool isDead;
public bool isGrounded;
public bool isJumping;
public bool isAttacking;
Quaternion targetRotation;
Transform cam;
Animator anim;
Rigidbody rb;
public Transform groundCheck;
public LayerMask whatIsGround;
Collider[] groundCollision;
void Start ()
{
cam = Camera.main.transform;
anim = GetComponentInChildren <Animator>();
rb = GetComponent<Rigidbody>();
Physics.gravity = nGravity;
ishurt = false;
isDead = false;
}
// Update is called once per frame
void Update ()
{
groundCollision = Physics.OverlapSphere(groundCheck.position, 0.2f, whatIsGround);
if (groundCollision.Length > 0)
{
isGrounded = true;
isJumping = false;
}
else
{
isGrounded = false;
isJumping = true;
}
GetInput();
}
private void FixedUpdate()
{
anim.SetBool("Grounded", isGrounded);
if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;
CalculateDirection();
Rotate();
Move();
}
void GetInput()
{
if (isGrounded && !ishurt && !isDead)
{
input.x = Input.GetAxis("Horizontal");
input.y = Input.GetAxis("Vertical");
}
if (input.x != 0 || input.y != 0) isMoving = true;
if (input.x == 0 && input.y == 0) isMoving = false;
anim.SetBool("Moving", isMoving);
if (Input.GetButtonDown("Jump") && isGrounded && !isJumping && !isAttacking)
{
PlayerJump();
}
anim.SetFloat("VSpeed", rb.velocity.y);
}
void CalculateDirection()
{
angle = Mathf.Atan2(input.x, input.y);
angle = Mathf.Rad2Deg * angle;
angle += cam.eulerAngles.y;
}
void Rotate()
{
targetRotation = Quaternion.Euler(0, angle, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime);
}
void Move()
{
if (!isAttacking && !ishurt && !isDead) transform.position += transform.forward * velocity * Time.deltaTime;
}
void PlayerJump()
{
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
public void IsAttack()
{
isAttacking = !isAttacking;
}
}