using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FinalMove : MonoBehaviour
{
private Rigidbody2D rig;
private Collider2D coll;
private Animator anim;
// public Collider2D discoll;//用于下蹲关闭头部碰撞体
public float speed, jumpforce;
public Transform groundcheck;//检测地面
// public Transform CellingCheck;//检测顶部
public LayerMask ground;
public bool isGround, isJump;
bool jumpPressed;//检验跳跃按键是否按下
int jumpCount;//跳跃次数
void Start()
{
rig = GetComponent<Rigidbody2D>();
coll = GetComponent<Collider2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if(Input.GetButtonDown("Jump")&&jumpCount>0)
{
jumpPressed = true;
}
}
private void FixedUpdate()
{
GroundMove();
Jump();
isGround = Physics2D.OverlapCircle(groundcheck.position, 0.1f, ground);
SwitchAnim();
}
void GroundMove()
{
//左右移动
float horizontalmove= Input.GetAxisRaw("Horizontal");//-1、0、1
rig.velocity = new Vector2(horizontalmove * speed, rig.velocity.y);
//面朝方向
if(horizontalmove != 0)
{
transform.localScale = new Vector3(horizontalmove, 1, 1);
}
}
void Jump()//跳跃
{
if(isGround)
{
jumpCount = 2;
isJump = false;
}
if (jumpPressed && isGround)
{
isJump = true;
rig.velocity = new Vector2(rig.velocity.x, jumpforce);
jumpCount--;
jumpPressed = false;
}
else if (jumpPressed && jumpCount > 0&&!isGround)
{
isJump = true;
rig.velocity = new Vector2(rig.velocity.x, jumpforce);
jumpCount--;
jumpPressed = false;
}
}
void SwitchAnim()//角色动画
{
anim.SetFloat("running", Mathf.Abs(rig.velocity.x));
if(isGround)
{
anim.SetBool("falling", false);
}
else if(!isGround&&rig.velocity.y>0)
{
anim.SetBool("jumping", true);
}
else if(rig.velocity.y<0)
{
anim.SetBool("jumping", false);
anim.SetBool("falling", true);
if(coll.IsTouchingLayers(ground))
{
anim.SetBool("falling", false);
anim.SetBool("idle", true);
}
}
}
/*void Crouch()//角色下蹲
{
if (!Physics2D.OverlapCircle(CellingCheck.position, 0.2f, ground))//判断蹲下后是否站起(没有障碍物时才执行)
{
if (Input.GetButton("Crouch"))
{
anim.SetBool("crouching", true);
discoll.enabled = false;//关闭 Box collider
}
else
{
anim.SetBool("crouching", false);
discoll.enabled = true;//打开 Box collider
}
}
}*/
}
一个字:绝!