想一次只拿起一把枪,但当我按下按钮时,玩家拿起了两把枪,所以请有人解决这个问题,我想在玩家按下特定键时只拿起一把枪,但当玩家放下枪时,只有玩家可以拿起另一把枪
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;
}
}
您可以添加变量,例如_gunpickduptimestamp。当玩家拿起枪赋值DateTime.Now。并在玩家每次拿起枪支时添加评估条件。就像
if ((_gunPickedUpTimeStamp - DateTime.Now) > TimeSpan.FromSeconds(5)) {
do something...
_gunPickedUpTimeStamp = DateTime.Now
}
我想你应该修改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;
}
}