SunnyLand官方案例
✨unity2d官方基础教学(主要是练习动画还有移动),文章如有误请指正,如果觉得对你有用,请点赞收藏关注一波,谢谢支持😘
提示:以下是本篇文章正文内容
Build分享
百度网盘:
链接:https://pan.baidu.com/s/1hMETms_uE9llAZ-r_JVy4Q
提取码:t9fl
效果图
代码
player
public static player Instance;
private float hor;
public float hor1;
private Rigidbody2D 刚体组件;
[HideInInspector]
public Collider2D 碰撞体组件;
public Collider2D 蹲下取消碰撞组件;
public float moveSpeed = 10f;
public float jumpSpeed = 10f;
public float bullts方向;
[HideInInspector]
public Animator 动画组件;
public LayerMask layerMask;
public AudioClip clip;
private bool 移动开关;
private bool 是否蹲下;
private bool 是否触碰蹲下的碰撞器;
private bool isWallUp;
private bool 是否碰到道具墙;
public bool IsWallUp { get => isWallUp;
set
{
isWallUp = value;
if (!是否碰到道具墙||!secretProp.Instance.isGo) return;
if (IsWallUp&&是否碰到道具墙)
{
secretProp.Instance.Index += 1;
是否碰到道具墙 = false;
}
}
}
void Start()
{
Instance = this;
刚体组件 = GetComponent<Rigidbody2D>();
动画组件 = GetComponent<Animator>();
碰撞体组件 = GetComponent<Collider2D>();
}
void FixedUpdate()
{
if (!移动开关)
{
移动();
}
动作();
}
/// <summary>
/// 按键做事情
/// </summary>
void 移动()
{
hor = Input.GetAxis("Horizontal");
hor1 = Input.GetAxisRaw("Horizontal");
if (hor != 0)
{
刚体组件.velocity = new Vector2(hor * Time.deltaTime * moveSpeed, 刚体组件.velocity.y);
}
if (hor1 != 0)
{
transform.localScale = new Vector3(hor1, 1, 1);
bullts方向 = hor1;
}
if (Input.GetButton("Jump") && 碰撞体组件.IsTouchingLayers(layerMask) && 是否触碰蹲下的碰撞器)
{
刚体组件.velocity = new Vector2(刚体组件.velocity.x, jumpSpeed * Time.deltaTime);
动画组件.SetBool("isJump", true);
IsWallUp = false;
}
//按下ctrl件暂时取消碰撞组件
if (Input.GetKey(KeyCode.LeftControl)&&!动画组件.GetBool("isLuo"))
{
是否蹲下 = true;
//防止bug
if (动画组件.GetBool("isCrouch"))
{
蹲下取消碰撞组件.enabled = false;
}
}
else
{
if (是否触碰蹲下的碰撞器)
{
是否蹲下 = false;
蹲下取消碰撞组件.enabled = true;
}
}
}
void 动作()
{
if (hor != 0)
{
动画组件.SetFloat("isRun", Mathf.Abs(hor));
}
if (动画组件.GetBool("isJump"))
{
if (刚体组件.velocity.y < 0)
{
动画组件.SetBool("isLuo", true);
动画组件.SetBool("isJump", false);
}
}
else if (移动开关)