using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClimbLadder : MonoBehaviour
{
private float vertical;//垂直输入
private float speed = 3f;//爬梯子的速度
private bool isLadder; //是否站在梯子上
private bool isClimbing;//是否在爬梯子的状态
Animator anim;
[SerializeField]
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
vertical = Input.GetAxis("Vertical");
if (isLadder==true&&Mathf.Abs(vertical)>0f) //在梯子上的时候 同时 垂直输入的绝对值Mathf.Abs 大于0 的时候
{
isClimbing = true;
}
}
private void FixedUpdate()
{
if (isClimbing==true)
{
rb.gravityScale = 0f;//爬梯子的时候重量比例为0
rb.velocity = new Vector2(rb.velocity.x,vertical*speed);
}
else
{
rb.gravityScale = 1f;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Ladder"))
{
isLadder = true;
isClimbing = true;
anim.SetBool("climbing", true);
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Ladder"))
{
isLadder = false;
isClimbing = false; //在更新退出梯子的时候触发攀爬为false
anim.SetBool("climbing", false);
}
}
}
Unity2D爬梯子demo
猜你喜欢
转载自blog.csdn.net/2402_83809362/article/details/142800000
今日推荐
周排行