有人在看这本书么第六章的案例分析先让设float 型变量speed和direction用来idle到run的状态过渡控制与人物从idle到run的状态过渡和bool型变量jump用来控制体跳跃状态
其中direction和speed给多少
这是让添加到角色的代码
using UnityEngine;
using System.Collections;
public class IdleRunJump : MonoBehaviour {
protected Animator animator;
public float DirectionDampTime = .25f;
public bool ApplyGravity = true;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
if(animator.layerCount >= 2)
animator.SetLayerWeight(1, 1);
}
// Update is called once per frame
void Update ()
{
if (animator)
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Base Layer.Run"))
{
if (Input.GetButton("Fire1")) animator.SetBool("Jump", true);
}
else
{
animator.SetBool("Jump", false);
}
if(Input.GetButtonDown("Fire2") && animator.layerCount >= 2)
{
animator.SetBool("Hi", !animator.GetBool("Hi"));
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
animator.SetFloat("Speed", h*h+v*v);
animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);
}
}
}
其中direction和speed给多少
这是让添加到角色的代码
using UnityEngine;
using System.Collections;
public class IdleRunJump : MonoBehaviour {
protected Animator animator;
public float DirectionDampTime = .25f;
public bool ApplyGravity = true;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
if(animator.layerCount >= 2)
animator.SetLayerWeight(1, 1);
}
// Update is called once per frame
void Update ()
{
if (animator)
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Base Layer.Run"))
{
if (Input.GetButton("Fire1")) animator.SetBool("Jump", true);
}
else
{
animator.SetBool("Jump", false);
}
if(Input.GetButtonDown("Fire2") && animator.layerCount >= 2)
{
animator.SetBool("Hi", !animator.GetBool("Hi"));
}
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
animator.SetFloat("Speed", h*h+v*v);
animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);
}
}
}