- 0.学习视频地址
- 1.零碎知识点
- 2.物体运动
- 3.添加动画
- 创建动画控制器
- 不同动画之间的切换
Unity2D入门M_studio
我自己学习时Unity的版本为2020.3.30f1c1
- scene的一个网格就是一个Unit,素材上有Pixels of Unit就是指每个网格放多少个像素。
- 玩家一般所需组件:Rigidbody 2D(刚体组件,可以调节重力),Box Collider 2D(碰撞体,可以编辑玩家的碰撞边界)。
- TileMap:Tilemap Collider 2D(碰撞组件,可地图里添加的物体添加碰撞)
- Edit→Project setting→Input Manger→Axes
用来管理移动啊,跳啊等动作的按键 - 物体运动过程中,如果发生翻滚,是因为没有关闭物体组件Rigidbody 2D→Constrains→Freeze Rotation Z,这个选项是管物体z轴旋转的
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public float moveSpeed=10f;
public float jumpForce;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
//GetAxis返回-1到1的小数
float horizontalMove = Input.GetAxis("Horizontal");
//GetAxisRaw返回-1,0,1
float faceDirection = Input.GetAxisRaw("Horizontal");
if (horizontalMove!=0)
{
rb.velocity = new Vector2(horizontalMove * moveSpeed, rb.velocity.y);
}
if (faceDirection!=0)
{
transform.localScale = new Vector3(faceDirection, 1, 1);
}
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}
3.添加动画
创建动画控制器
- 给物体添加动画组件Animator
- 在Assets文件夹新建Animation文件夹用来管理动画,再新建Player文件夹,用来管理玩家的动画,再在Player文件夹中新建Animator Controller用来控制动画,并将其拖给物体Animator组件上的controller。
Assets文件夹→Animation文件夹→Player文件夹→Animator Controller动画控制器 - 打开Windows→Animation→Aniamtion,选中游戏对象,新建动画名称,要在Palyer文件夹中,拖拽素材形成动画。
可能出现的问题:动画在闪烁
解决方案:调整物体物件Spring Renderer中的sorting layer,重叠关系会遮挡闪烁
windows→Animation→Animator
可以看到不同动画之间的关系,也可以添加不同动画之间的关系,也可以添加参数,条件来调节不同动画之间的关系。
动画之间的关系:
动画之间的切换及条件:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public float moveSpeed=10f;
public float jumpForce;
public Animator anim;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Movement();
}
void Movement()
{
//GetAxis返回-1到1的小数
float horizontalMove = Input.GetAxis("Horizontal");
//GetAxisRaw返回-1,0,1
float faceDirection = Input.GetAxisRaw("Horizontal");
if (horizontalMove!=0)
{
rb.velocity = new Vector2(horizontalMove * moveSpeed, rb.velocity.y);
anim.SetFloat("running", Mathf.Abs(faceDirection));
}
if (faceDirection!=0)
{
transform.localScale = new Vector3(faceDirection, 1, 1);
}
if (Input.GetButtonDown("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x , jumpForce);
}
}
}



