我試圖讓我的玩家在我按下 E 時撿起一個物品,并在我再次按下 E 時放下它。我希望物品出現在玩家的手中。玩家應該只能在一定距離內撿起物品。我當前的 PickUP 腳本在這里,目的地附加到玩家手上。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour
{
public int number = 1;
public Transform theDest;
public void Update()
{
if (Input.GetKeyUp(KeyCode.E) && (number % 2) == 1)
{
GetComponent<BoxCollider>().enabled = false;
GetComponent<Rigidbody>().useGravity = false;
GetComponent<Rigidbody>().isKinematic = true;
this.transform.position = theDest.position;
this.transform.parent = GameObject.Find("Destination").transform;
number = number 1;
}
else if (Input.GetKeyUp(KeyCode.E) && (number % 2) == 0)
{
GetComponent<Rigidbody>().isKinematic = false;
GetComponent<BoxCollider>().enabled = true;
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
number = number - 1;
}
}
}
我做了一些研究,但我很困惑,無法弄清楚如何在一定距離內撿起物品。我按照教程中的腳本進行操作,但他們正在做第一人稱游戲并使用“光線投射”。如果有人可以幫我弄清楚如何在一定距離內撿起物品,甚至可以幫助我想出另一種方式在第三個人中撿起物品,我將不勝感激。我試圖盡可能多地澄清,但如果您需要更多資訊,請告訴我。我是新來的,不知道該包括什么。(如果有幫助,我正在使用 Unity Invector-3rd Person 控制器 Lite)
uj5u.com熱心網友回復:
您可以制作一個具有一定半徑的以圓形玩家為中心的盒子碰撞器,并使用 Oncollisionenter 和 OnCollisionExit 來知道游戲物件是否在玩家旁邊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/362085.html
上一篇:如何為增量游戲制作關卡購買乘數?
