目录
拖动一个素材,重命名为Robot,以
创建一个机器人对象
为机器人建立动画
双击打开
角色拖入
设定帧数率
同理,已有Down,现制作Up,Left和Right
命名技巧:点击上面的文件名,然后稍微修改一下
Right是由Left翻转得到的
所以需要对其进行处理
单独选择该动画是无法添加属性的,必须在对象上进行操作才可以
在动画面板中
逐一修改帧数
删除这四个动画状态
创建一个新混合树
添加上下左右四个运动
添加参数
修改树的类型还有参数
上 Y 1
下 Y -1
左 X -1
右 X 1
创建机器人控制脚本
机器人要有碰撞,所以为机器人添加刚体和碰撞体
刚体里面重力影响取消掉
编辑碰撞盒
继续写脚本
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; public class RobotControl : MonoBehaviour { public float speed = 3f;//机器人要运动肯定要有一个速度 Animator anim;//创建一个动画状态集的变量,用来设定动画状态集的值,利用Animator参数,要决定到底播放左边的动画还是右边的动画 Rigidbody2D rbody;//为了防止角色抖动,用刚体的moveposition来确定位置 public bool isVertical;//设定一个参数,判断是水平移动还是垂直移动 Vector2 moveDirection;//设定一个运动方向的变量 float changeTimer;//设定一个计时器,每过一段时间,机器人换一个方向运动 public float changeDirectionTime = 2f;//计时器长度 // Start is called before the first frame update void Start() { anim = GetComponent<Animator>();//获取动画组件,一会儿要修改它的参数 rbody = GetComponent<Rigidbody2D>();//获取2d刚体,一会儿要修改它的位置 moveDirection = isVertical ? Vector2.up : Vector2.right;//初始状态下判断运动方向 changeTimer = changeDirectionTime;//计时器初始化 } // Update is called once per frame void Update() { changeTimer -= Time.deltaTime;//计时器每一帧都减小 if(changeTimer<0) { moveDirection *= -1;//如果小于0,改变方向 changeTimer = changeDirectionTime;//改变方向后计时器重新初始化 } Vector2 position = rbody.position;//定义一个二维向量位置坐标来获取刚体坐标 position.x += moveDirection.x * speed * Time.deltaTime;//改变水平方向位置 position.y += moveDirection.y * speed * Time.deltaTime;//改变垂直方向位置 rbody.MovePosition(position);//为了防止抖动,调用刚体的moveposition,里面是二维向量的位置 //设定动画状态集的参数,把moveDirection的值传递给move X/Y anim.SetFloat("move X", moveDirection.x); anim.SetFloat("move Y", moveDirection.y); } }
脚本编辑好之后绑定到角色上面
机器人只能原地变换方向,没有位移,好奇怪
错误代码:
不是 moveDirection.x 而是 position.x
moveDirection.x += moveDirection.x * speed * Time.deltaTime;//改变水平方向位置 moveDirection.y += moveDirection.y * speed * Time.deltaTime;//改变垂直方向位置
机器人撞树翻了,然后一直旋转的解决方法
冻结旋转
创建预制体
先建一个,再以它再建一个变体,调整一下方向,这样就有两个方向的怪
创建雷区
先把这两个机器人藏起来
现在就是要让玩家踩到雷区,然后让两个怪再出来
创建
把预制体文件夹放进去
编辑 PlayerControl.cs 脚本
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; public class PlayerControl : MonoBehaviour { public float speed = 8f; Rigidbody2D rbody;//定义2d刚体 Animator anim;//定义动画 Vector2 lookDirection = new Vector2(1, 0);//初始向x看 GameObject robotObj;//定义一个机器人对象 // Start is called before the first frame update void Start() { rbody = GetComponent<Rigidbody2D>();//获取2d刚体 anim = GetComponent<Animator>();//获取动画组件 } // Update is called once per frame void Update() { float moveX = Input.GetAxisRaw("Horizontal"); float moveY = Input.GetAxisRaw("Vertical"); Vector2 moveVector = new Vector2(moveX, moveY); if(moveX!=0||moveY!=0) { lookDirection = moveVector; } Vector2 position = transform.position; position.x += moveX * speed * Time.deltaTime; position.y += moveY * speed * Time.deltaTime; transform.position = new Vector2(position.x, position.y); rbody.MovePosition(position);//用刚体移动可以消除画面抖动 anim.SetFloat("move X",lookDirection.x);//为动画集赋值 anim.SetFloat("move Y",lookDirection.y); anim.SetFloat("runspeed", moveVector.magnitude);//状态集的切换由运动矢量决定 //赋值必须与状态集命名一样 } private void OnTriggerEnter2D(Collider2D collision) { float robotX = Random.Range(-3, 5); float robotY = Random.Range(-3, 5); Vector2 position_robot = new Vector2(robotX, robotY); if(collision.gameObject.tag=="bomb")//如果碰到雷区 { print("other.tag=" + collision.gameObject.tag);//这句一定要有,不然出不来 robotObj = Instantiate(Resources.Load("Prefabs/Robot"),position_robot,Quaternion.identity)as GameObject;//加载资源,生成一个预制体,做成一个对象 } } private void OnTriggerExit2D(Collider2D collision) { if(collision.gameObject.tag=="bomb") { Destroy(robotObj); } } }
复制路径
预制体路径 Assets / Resources / Prefabs / Robot.prefab robotObj=Instantiate(Resources.Load(""))//加载资源用预制体克隆一个对象 >>删去不必要的路径 robotObj = Instantiate(Resources.Load("Assets / Resources / Prefabs / Robot.prefab")); >> robotObj = Instantiate(Resources.Load("Prefabs / Robot")); >>填写其他参数,如随机生成的位置,还有方向,去掉空格 robotObj = Instantiate(Resources.Load("Prefabs/Robot"),position_robot,Quaternion.identity)as GameObject;
若路径不对会有如下报错
The Object you want to instantiate is null.
为雷区对象设定标签bomb
为雷区添加碰撞体
并指定为触发器
效果:
如果从雷区里出来了,就把机器人销毁掉
private void OnTriggerExit2D(Collider2D collision) { if(collision.gameObject.tag=="bomb") { Destroy(robotObj); } }
(完)