一、添加动画器
![](https://img-blog.csdnimg.cn/20210903152746801.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6aOe6bG85YiS6L-H5pif56m6,size_9,color_FFFFFF,t_70,g_se,x_16)
?并为动画器添加控制器
![](https://img-blog.csdnimg.cn/20210903153024416.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6aOe6bG85YiS6L-H5pif56m6,size_20,color_FFFFFF,t_70,g_se,x_16)
?在动画器中添加动画(ider),(run)并创建过渡,添加bool类型参数ider和run
![](https://img-blog.csdnimg.cn/20210903153259218.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6aOe6bG85YiS6L-H5pif56m6,size_20,color_FFFFFF,t_70,g_se,x_16)
?
设置过度条件
![](https://img-blog.csdnimg.cn/20210903153727776.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA6aOe6bG85YiS6L-H5pif56m6,size_20,color_FFFFFF,t_70,g_se,x_16)
?
二、添加代码
在上一节的代码中添加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//速度
public int speed;
//刚体
Rigidbody2D player;
//动画
Animator anim;
//位置
Vector2 moveX;
void Start()
{
player = this.GetComponent<Rigidbody2D>();
anim = this.GetComponent<Animator>();
}
private void FixedUpdate()
{
Ider();
Run();
}
//等待
private
void Ider()
{
moveX.x = Input.GetAxisRaw("Horizontal");
anim.SetBool("ider", true);
anim.SetBool("run", false);
}
//跑步
private void Run()
{
if (moveX.x != 0)
{
//人物根据移动方向旋转
transform.localScale = new Vector3(moveX.x, 1, 1);
//MovePosition()方法:自身位置+moveX * Time.fixedDeltaTime * speed
player.MovePosition(player.position + moveX * Time.fixedDeltaTime * speed);
//动画
anim.SetBool("run", true);
anim.SetBool("ider", false);
}
}
}
?三、运行游戏
|