Animation Help!
So i have this animation but i wanted to know how to apply it to my walk script. The last code i used it said that the animation could not be played because it could not be found. But anyways here is the script, please help, thanks!
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public bool controllable = true;
public float speed = 7.0f;
public float jumpSpeed = 6.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded && controllable)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Answer by rajan4uto · Dec 31, 2015 at 12:23 PM
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public bool controllable = true;
**public Animator anim;**
public float speed = 7.0f;
public float jumpSpeed = 6.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
private CharacterController controller;
// Use this for initialization
void Start()
{
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
if (controller.isGrounded && controllable)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
**anim.setFloat("yourvariablename",moveDirection);**
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
you have to define moveDirection in animator in transition from any state to walk that if moveDirection is greater than 0.1 than transit to walk animation and in return moveDirection is less than 0.1.
error CS1502: The best overloaded method match for UnityEngine.Animator.SetFloat(string, float)' has some invalid arguments error CS1503: Argument
#2' cannot convert UnityEngine.Vector3' expression to type
float'
use float value, just check when your movedirection value increase from 0.1. Don't use bool in argument and a float argument and use "greaterthan 0.1" to make transition from any state to walk state.