角色运动和动画制作
1. 新建场景和加入角色
1.首先从官网下载Ruby’s Adventure资源包,导入到项目中后,开始创建场景。一般,我们将游戏中的第一个场景命名为MainGame。
2.新建场景后,双击MainGame,出现白色方框,表示场景已经创建完成。找到Ruby面向我们的图像添加到场景中,重命名为Player,方便后续用到Tag时不会忘记。
3.出于习惯,紧接着给Player添加 Rigidbody 2D组件,注意不能选成Rigidbody组件。
4.本游戏中,Ruby的行动由我们的键盘所控制,因此将重力设为0,并固定Z轴,防止后续添加碰撞后发生旋转。
5.为Player创建PlayerController脚本,命名一般采取对象名+Controller,表明谁被控制。添加新的文件夹Scripts用于存放脚本,将PlayerController拖入文件夹中。
6.双击脚本进入编辑器,编写Ruby移动的代码。
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 5f;//习惯使然,在5后加f表示浮点数
private Rigidbody2D _rigidbody2D;//创建刚体,不需外界看见,所以设为private
private Vector2 _lookDirection = Vector2.down;//设置Ruby的初始朝向
private Vector2 _currentInput;//存储我们的键盘输入
private float _x;
private float _y;
// Start is called before the first frame update
void Start()
{
_rigidbody2D = GetComponent<Rigidbody2D>