所以,我制作了一个让玩家爬上梯子的脚本。我正在使用“FREE Footstep System”Unity资产的略微修改的FPS预制件。问题是,当你爬上梯子时,一切正常,但是当你走下梯子时,你可以走到地板下。另外,当你离开扳机时,玩家向前移动一点以避免碰到。因此,执行防粘功能,但其他一切都没有。如何解决?
UPD:哎呀,忘了密码!using UnityEngine;
using System.Collections;
public class ClimbOnLadder : MonoBehaviour {
public bool isClimbing = false;
private Transform playerTransform;
private Vector3 newPlayerPosition;
void OnTriggerEnter(Collider colliderEntered)
{
if (colliderEntered.gameObject.CompareTag("Player"))
{
isClimbing = true;
colliderEntered.gameObject.GetComponent<CharacterController>().enabled = false;
colliderEntered.gameObject.GetComponent<CapsuleCollider>().enabled = true;
colliderEntered.gameObject.GetComponent<Footsteps.FirstPersonController>().enabled = false;
colliderEntered.gameObject.GetComponent<Footsteps.CharacterFootsteps>().enabled = false;
playerTransform = colliderEntered.gameObject.GetComponent<Transform>();
}
}
void OnTriggerExit(Collider colliderLeft)
{
if (colliderLeft.gameObject.CompareTag("Player"))
{
isClimbing = false;
newPlayerPosition.Set(gameObject.GetComponent<Transform>().localPosition.x + 0.125F, playerTransform.localPosition.y + 0.25F, playerTransform.localPosition.z);
playerTransform.position = newPlayerPosition;
colliderLeft.gameObject.GetComponent<CharacterController>().enabled = true;
colliderLeft.gameObject.GetComponent<CapsuleCollider>().enabled = false;
colliderLeft.gameObject.GetComponent<Footsteps.FirstPersonController>().enabled = true;
colliderLeft.gameObject.GetComponent<Footsteps.CharacterFootsteps>().enabled = true;
playerTransform = null;
newPlayerPosition.Set(0, 0, 0);
}
}
void FixedUpdate()
{
if (isClimbing)
{
newPlayerPosition.Set(playerTransform.localPosition.x, playerTransform.localPosition.y + (Input.GetAxis("Vertical")*0.0675F), playerTransform.localPosition.z);
playerTransform.localPosition = newPlayerPosition;
}
}
}
答案 0 :(得分:0)
哇。不知怎的,我通过修复对撞机的位置,部分地设法修复了这个bug。现在整个OnTriggerExit函数都有效。耶!
UPD:完全解决问题。删除了反粘贴代码,因为它不再需要了,并且将梯子移得更高。