theLevel = FindObjectOfType<LevelManager>();//另一种写法:public LevelManager theLevel
isGround = Physics2D.OverlapCircle(checkPoint.position, radius, layerGround);//地面检测
//角色移动(2D)
void FixedUpdate()
{
if (Input.GetAxisRaw("Horizontal") > 0)
{
rb.velocity = new Vector2(speed, rb.velocity.y);
this.transform.localScale = new Vector3(1, 1, 1);
}
else if (Input.GetAxisRaw("Horizontal") < 0)
{
rb.velocity = new Vector2(-speed, rb.velocity.y);
this.transform.localScale = new Vector3(-1, 1, 1);
}
else
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
if (Input.GetAxisRaw("Jump") > 0 && isGround)
{
rb.velocity = new Vector2(rb.velocity.x, jumpSpeed);
}
}
anim.SetFloat("speed",Mathf.Abs(rb.velocity.x)); //设定animation speed值,大于0.1f后动画切换为跑
anim.SetBool("grounded",isGround);
if (isInvincible)
{
timeSpentInvincible += Time.deltaTime; //计时
if (timeSpentInvincible < 3f) //小于3秒闪烁
{
float remainder = timeSpentInvincible % 0.3f;
GetComponent<Renderer>().enabled = remainder > 0.15f;
}
else
{
GetComponent<Renderer>().enabled = true;
isInvincible = false;
timeSpentInvincible = 0;
}
public void Respawn() //死亡协程
{
thePlayer.isInvincible = false;
thePlayer.GetComponent<Renderer>().enabled = true;
StartCoroutine("RespawnCo");
}
public IEnumerator RespawnCo()
{
thePlayer.gameObject.SetActive(false);
Instantiate(deadEffect,thePlayer.transform.position,thePlayer.transform.rotation);
yield return new WaitForSeconds(respawnDelay);
thePlayer.gameObject.transform.position = thePlayer.nowRebirthVector2;
healthCount = healthMax;
UpdataHealth();
thePlayer.gameObject.SetActive(true);
}
void Update () {
//镜头跟随
targetPos= new Vector3(player.transform.position.x,transform.position.y,transform.position.z);
if (player.transform.localScale.x>0)
{
targetPos = new Vector3(player.transform.position.x+ahead, transform.position.y, transform.position.z);
}
else if (player.transform.localScale.x <0)
{
targetPos = new Vector3(player.transform.position.x-ahead, transform.position.y, transform.position.z);
}
transform.position=Vector3.Lerp(transform.position,targetPos,smooth*Time.deltaTime);//顺滑过渡
}
void Update (){ //平台往复
moveThings.transform.position = Vector2.MoveTowards(moveThings.transform.position, targetPoint, moveSpeed * Time.deltaTime);
if (moveThings.transform.position == startPoint.position)
{
targetPoint = endPoint.position;
}
else if (moveThings.transform.position == endPoint.position)
{
targetPoint = startPoint.position;
}
}