1. 将准备好的素材--变成16像素点-- >Fliter Mode--Point -- >Compression(压缩)--None
2.将素材切割-- Sprite Editor--切割--可以按照像素点切割地图--切割人物可以数格数
3.制作背景-->窗口-->2D-->平铺调色板(Palette)-->拖到检查器上-->将准备好的地图放入平铺调色板(注意:新建文件夹)--在层级中创建2D--矩形
4.图层之间--后图层会遮挡前面的图层
5.制作一个Rule Tiles--2D瓦片-Rule --然后选中刚刚切割的地图--然后定义图片出现的规则
二:
1.假如想用新的Input系统--> 进ProjectSeting-->Player-->API兼容级别(.NetFrameWork)-->输入系统(新) -->在包管理器中安装Input System-->在Setting中创建文件夹-->InputAction
创建组件PlayerInput让他生成代码--在自己代码里面调用
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public PlayerInputControl inputControl;
public Vector2 inputDirection;
private void Awake()
{
inputControl = new PlayerInputControl();
}
private void OnEnable()
{
inputControl.Enable();
}
private void OnDisable()
{
inputControl.Disable();
}
private void Update()
{
inputDirection = inputControl.Gameplay.Move.ReadValue<Vector2>();
}
}
行动+ 翻身
private void FixedUpdate()
{
//固定时钟执行
Move();
}
public void Move()
{
m_rigidbody.velocity = new Vector2(inputDirection.x * speed * Time.deltaTime, m_rigidbody.velocity.y);
//强制转换
int faceDir = (int)transform.localScale.x;
//人物翻转
if (inputDirection.x > 0)
faceDir = 1;
if (inputDirection.x < 0)
faceDir = -1;
m_transform.localScale = new Vector3(faceDir, 1, 1);
}
}
检测地面--把地面新弄为一个Grond层--然后public一个layerMask
public bool isGround;
public float checkRaduis;
public LayerMask groundLayer;
void Update()
{
Check();
}
public void Check()
{
//检测地面
isGround = Physics2D.OverlapCircle(transform.position, checkRaduis, groundLayer);
}
重点:两代码之间互相访问
public Physicscheck physicscheck;
physicscheck = GetComponent<Physicscheck>();
人物跳跃
private void Jump(InputAction.CallbackContext obj)
{
//碰地才能跳跃
if(physicscheck.isGround)
m_rigidbody.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);//瞬时的力
}
给角色光滑物理材质--物理材质Friction--0
创建基本动画-- 创建Animator --然后再创建Animation --用代码控制转换条件
public void SetAnimation()
{
anim.SetFloat("velocityX", Mathf.Abs(rb.velocity.x));
anim.SetFloat("velocityY", rb.velocity.y);
anim.SetBool("isGround", physicscheck.isGround);
anim.SetBool("isCrouch", playerController.isCrouch);
}
角色攻击
public class Character : MonoBehaviour
{
[Header("基本属性")]
public float maxHealth;
public float currentHealth;
[Header("受伤无敌")]
public float invulnerableDurnation;
public float invulnerableCounter;
public bool invulnerable;
private void Start()
{
currentHealth = maxHealth;
}
private void Update()
{
if (invulnerable)
{
//计时器
invulnerableCounter -= Time.deltaTime;
if(invulnerableCounter<= 0)
{
invulnerable = false;
}
}
}
public void TakeDanage(Attack attacker)
{
//如果无敌就不执行了
if (invulnerable)
return;
//Debug.Log(attacker.damage);
if (currentHealth - attacker.damage > 0)
{
currentHealth -= attacker.damage;
TriggerInvlneralbe();
}
else
{
currentHealth = 0;
}
//受伤之后--无敌
TriggerInvlneralbe();
}
private void TriggerInvlneralbe()
{
if (!invulnerable)
{
invulnerable = true;
invulnerableCounter = invulnerableDurnation;
}
}
}
配套
public class Attack : MonoBehaviour
{
public int damage;
public float attakrange;
public float attackrate;
private void OnTriggerStay2D(Collider2D other)
{
other.GetComponent<Character>()?.TakeDanage(this);
}
}
抽象类的下面只写方法的声明--但是不写具体实现