Код: Выделить всё
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController2D controller;
float horizontalMove = 0f;
float verticalMove = 0f;
public float runSpeed = 40f;
bool jump = false;
public Animator animator;
private bool isLadder = false;
public Rigidbody2D rigidBody2D;
Collider2D tempCollision;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
}
// Update is called once per frame
void Update()
{
horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
verticalMove = Input.GetAxisRaw("Vertical") * runSpeed;
animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
//Debug.Log("isLadder:" + isLadder);
if(Input.GetButtonDown("Left") || Input.GetButtonDown("Right"))
{
//Debug.Log("Left or Right");
animator.SetBool("Climb", false);
}
if (Input.GetButtonDown("Up") && isLadder)
{
//Debug.Log("Up");
animator.SetBool("Climb", true);
}
if (Input.GetButtonDown("Jump") && !isLadder)
{
jump = true;
}
}
void OnTriggerEnter2D(Collider2D collision)
{
//Debug.Log("Object that collided with me: " + collision.gameObject.tag);
if (collision.gameObject.tag == "Ladder" || collision.gameObject.tag == "Ladder_Bottom")
{
tempCollision = collision;
//Debug.Log(collision.gameObject.name);
isLadder = true;
}
}
void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ladder" || collision.gameObject.tag == "Ladder_Bottom")
{
tempCollision = null;
//Debug.Log(collision.gameObject.name);
animator.SetBool("Climb", false);
isLadder = false;
}
}
public void OnLanding()
{
animator.SetBool("IsJumping", false);
}
private void FixedUpdate()
{
controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
jump = false;
//isLadder = false;
}
}
< /code>
Как видите, я настраивает переменную аниматора на false для подъема, как только пользователь попадет в левую или правую клавишу: < /p>
if(Input.GetButtonDown("Left") ||
Input.GetButtonDown("Right"))
{
//Debug.Log("Left or Right");
animator.SetBool("Climb", false);
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... the-ladder