学习内容来自 B站up : M_Studio
文中使用图片素材来自unity素材库 2D- Sunnyland
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public Rigidbody2D rb;
public Animator Anim;
public Collider2D coll;
public float speed;
public float jumphigh;
public LayerMask Ground;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
Playermove();
SwitchAnim();
}
void Playermove()
{
float Horizontalmove;
float Facedirection;
Horizontalmove = Input.GetAxis("Horizontal");
Facedirection = Input.GetAxisRaw("Horizontal");
// 移动
if (Facedirection != 0)
{
transform.localScale = new Vector3(Facedirection, 1, 1);
}
if (Horizontalmove != 0)
{
Anim.SetBool("running", true);
rb.velocity = new Vector2(Horizontalmove * speed * Time.fixedDeltaTime, rb.velocity.y);
}
else if (Horizontalmove == 0)
{
Anim.SetBool("running", false);
}
// 跳跃
if (Input.GetButton("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumphigh * Time.fixedDeltaTime);
Anim.SetBool("jumping", true);
}
}
void SwitchAnim()
{
if (rb.velocity.y < -0.1)
{
Anim.SetBool("falling", true);
Anim.SetBool("jumping", false);
} else if (coll.IsTouchingLayers(Ground))
{
Anim.SetBool("falling", false);
}
/*if (!Anim.GetBool("jumping") && rb.velocity.y == 0)
{
Anim.SetBool("falling", false);
Anim.SetBool("idle", true);
}*/
}
}
float Horizontalmove;
float Facedirection; // 利用scale配合horizontal达成角色朝向的调整
Horizontalmove = Input.GetAxis("Horizontal");
Facedirection = Input.GetAxisRaw("Horizontal");
if (Facedirection != 0)
{
transform.localScale = new Vector3(Facedirection, 1, 1);
}
if (Horizontalmove != 0)
{
Anim.SetBool("running", true);
rb.velocity = new Vector2(Horizontalmove * speed * Time.fixedDeltaTime, rb.velocity.y);
}
else if (Horizontalmove == 0)
{
Anim.SetBool("running", false);
}
float Facedirection;
Facedirection = Input.GetAxisRaw("Horizontal");
……
transform.localScale = new Vector3(Facedirection, 1, 1);
利用scale配合horizontal达成角色朝向的调整。
此处getaxisraw得到的是-1,0,1,而用getaxis得到的是变动的浮点数。
public Rigidbody2D rb;
public float speed;
………
void FixedUpdate()
{
Playermove();
SwitchAnim();
}
…………
if (Horizontalmove != 0)
{
Anim.SetBool("running", true);
rb.velocity = new Vector2(Horizontalmove * speed * Time.fixedDeltaTime, rb.velocity.y);
}
else if (Horizontalmove == 0)
{
Anim.SetBool("running", false);
}
fixedupdate以时间为单位更新,默认为0.02,每秒50次,update则是以帧数更新,使用fixedupdate保证游戏运行在 帧数不同的情况下角色移动速度都一样。
*Time.fixedDeltaTime配合fixedupdate使用,数值固定,默认=0.02。
speed为速度参数,在unity-script中更改。
rb.velocity更改刚体的速度参数,new Vector2 对2D更改。 .y .x 为x,y轴速度。
Anim.SetBool(“running”, false) 更改移动判定以改变动画。
public Animator Anim;
public Collider2D coll;
public LayerMask Ground;
…………
if (Input.GetButton("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, jumphigh * Time.fixedDeltaTime);
Anim.SetBool("jumping", true);
}
…………
void SwitchAnim()
{
if (rb.velocity.y < -0.1)
{
Anim.SetBool("falling", true);
Anim.SetBool("jumping", false);
} else if (coll.IsTouchingLayers(Ground))
{
Anim.SetBool("falling", false);
}
/*if (!Anim.GetBool("jumping") && rb.velocity.y == 0)
{
Anim.SetBool("falling", false);
Anim.SetBool("idle", true);
}*/
}
在unity中用三个参数“jumping、falling、runing”来判定4种动画的衔接(闲站,跑动,跳起,下落);
因为左移用的是右移动画的翻转,在实际游戏中,左移时y值会有变动(略<0),所以判定句用 y < -0.1 。
先对y轴速度<0判断,便于在非跳跃下落(例如跑出平台掉落)时的动画转换。
public Collider2D coll;
public LayerMask Ground;
…………
if (coll.IsTouchingLayers(Ground))
用于判断落地以及碰撞墙体,由于上述原因,不使用y == 0 判断是否落地。(注释代码为发现问题前写的y = 0落地判定)