1在Project Setting的InputManager里可以创建任意新的按键
2.如何实现玩家光线投射
public class PlayerCasting : MonoBehaviour
{
public static float DistanceFromTarget;//static作用:变相的在inpsector里隐藏DistanceFromTarget,且各个脚本之间互相引用。
public float ToTarget;
void Update()
{
RaycastHit Hit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Hit))
{
ToTarget = Hit.distance;
DistanceFromTarget = ToTarget;
}
}
}
3.打开门的代码
public class DoorCellOpen : MonoBehaviour
{
public float TheDistance;
public GameObject ActionDisplay;
public GameObject ActionText;
public GameObject TheDoor;
public AudioSource CreakSound;
void Update()//实时更新的板块
{
TheDistance = PlayerCasting.DistanceFromTarget;
}
void OnMouseOver()
{
if (TheDistance <= 2)
{
ActionDisplay.SetActive(true);
ActionText.SetActive(true);
}
if (Input.GetButtonDown("Action"))
{
if (TheDistance <= 2)
{
this.GetComponent<BoxCollider>().enabled = false;
ActionDisplay.SetActive(false);
ActionText.SetActive(false);
TheDoor.GetComponent<Animation>().Play("FirstDoorOpenAnim");
CreakSound.Play();
}
}
}
void OnMouseExit()
{
ActionDisplay.SetActive(false);
ActionText.SetActive(false);
}
}