首页 > 解决方案 > 想拿起工具或枪支,但想在统一 c# 中一次只选择一个

问题描述

想一次只捡起一把枪,但当我按下按钮时玩家正在捡起两把枪,所以请有人解决这个问题我想在玩家按下特定键但玩家放下枪时只捡起一把枪然后唯一的玩家可以选择另一把枪

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;
    }
}

标签: c#unity3d2dgame-engine

解决方案


我猜你应该在这两种方法中修改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;
        }
    }

推荐阅读