想一次只拿起一把槍,但當我按下按鈕時,玩家同時拿起了兩把槍,所以請有人解決這個問題我想在玩家按下特定鍵時只拿起一把槍,但當玩家放下槍時下來然后唯一的玩家可以選擇另一把槍
public void pickup() //this is a function when player presses to pick up guns
{
if (Input.GetKeyDown(KeyCode.E) && Vector3.Distance(s.transform.position, transform.position) <= 5 && dontpickup)
{
//var a = gameObject.FindWithTag("player");
transform.SetParent(s);
Vector3 temp = transform.position;
temp.x = player.transform.position.x;
temp.y = player.transform.position.y;
transform.position = temp new Vector3(4, 0, 0);
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.isKinematic = true;
Collider2D c = GetComponent<Collider2D>();
c.isTrigger = true;
needdropdown = true;
}
}
public void dropdown() //this is a function when player presses to drop down the guns
{
if (Input.GetKeyDown(KeyCode.Q) && needdropdown)
{
//var a = gameObject.FindWithTag("player");
transform.SetParent(null);
Rigidbody2D rb = GetComponent<Rigidbody2D>();
rb.isKinematic = false;
Collider2D c = GetComponent<Collider2D>();
c.isTrigger = false;
}
}
void OnTriggerStay2D(Collider2D trig)
{
if(f.gameObject != null && gameObject.tag == "player")
{
dontpickup = true;
}
}
uj5u.com熱心網友回復:
您可以添加變數,例如 _gunPickedUpTimeStamp。當玩家拿起槍時分配值 DateTime.Now。并添加每次玩家拿起槍時評估的條件。就像是
if ((_gunPickedUpTimeStamp - DateTime.Now) > TimeSpan.FromSeconds(5)) {
do something...
_gunPickedUpTimeStamp = DateTime.Now
}
uj5u.com熱心網友回復:
我猜你應該在這兩種方法中修改dontpickup的值:
public void pickup() //this is a function when player presses to pick up guns
{
if (Input.GetKeyDown(KeyCode.E) && Vector3.Distance(s.transform.position, transform.position) <= 5 && dontpickup)
{
//before code
needdropdown = true;
dontpickup = false;
}
}
public void dropdown() //this is a function when player presses to drop down the guns
{
if (Input.GetKeyDown(KeyCode.Q) && needdropdown)
{
//before code
needdropdown = false;
dontpickup = true;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336857.html
