C# 当我抓住同一个物体时,如何投掷

C# 当我抓住同一个物体时,如何投掷,c#,unity3d,C#,Unity3d,这不在FPS中。这就像是一个自上而下的视图游戏。我必须在掷骰子时加入光线投射吗 我以为代码在void toss()下,但它不起作用。请帮忙 public Transform theDest; public float throwforce = 300f; void Update() { pickingoff(); toss(); } private void OnTriggerStay(Collider other) { if(other.gameObject.t

这不在FPS中。这就像是一个自上而下的视图游戏。我必须在掷骰子时加入光线投射吗

我以为代码在void toss()下,但它不起作用。请帮忙

public Transform theDest;
public float throwforce = 300f;


void Update()
{
    pickingoff();
    toss();
}


private void OnTriggerStay(Collider other)
{
    if(other.gameObject.tag == "Player")
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            GetComponent<Rigidbody>().useGravity = false;
            this.transform.position = theDest.transform.position;
            this.transform.parent = GameObject.Find("Destination").transform;
            Debug.Log("Pick up");
        }
    }
}


void toss()
{
    if(Input.GetMouseButtonDown(0))
    {
        GetComponent<Rigidbody>().AddForce(transform.forward * throwforce);
        GetComponent<Rigidbody>().useGravity = true;
        this.transform.parent = null;

    }

}

void pickingoff()
{
    if(Input.GetMouseButtonDown(1))
    {
        this.transform.parent = null;
        GetComponent<Rigidbody>().useGravity = true;
    }
}
public-Transform-theDest;
公共浮力=300f;
无效更新()
{
pickingoff();
投掷();
}
私人空间OnTriggerStay(碰撞器其他)
{
如果(other.gameObject.tag==“玩家”)
{
if(Input.GetKeyDown(KeyCode.Space))
{
GetComponent().useGravity=false;
this.transform.position=dest.transform.position;
this.transform.parent=GameObject.Find(“Destination”).transform;
Debug.Log(“拾取”);
}
}
}
空投
{
if(Input.GetMouseButtonDown(0))
{
GetComponent().AddForce(transform.forward*throwforce);
GetComponent().useGravity=true;
this.transform.parent=null;
}
}
void pickingoff()
{
if(Input.GetMouseButtonDown(1))
{
this.transform.parent=null;
GetComponent().useGravity=true;
}
}

要使某个对象在统一中移动,您必须向其添加方向和力,而且您似乎已经在这样做了,我担心您得到了对错误对象的引用,顺便说一句,请尝试为要抛出的对象指定一个标记,并使用类似于

var thingToThrow = GameObject.FindWithTag("ThingToThrow");
thingToThrow.mass = 10f;
thingToThrow.useGravity = true;
thingToThrow.AddForce(transform.forward * throwforce);

另一个主要优点是,你可以检查你正在增加的质量和力

这是统一制造的吗?是的。它在Unity里它能用吗?你能检查你的变量吗?它不见了!