gpt4 book ai didi

c# - 抓取和放下物体

转载 作者:行者123 更新时间:2023-11-30 23:31:02 25 4
gpt4 key购买 nike

我正在尝试新事物,并且一直在寻找一种拖放对象的方法。我的玩家是一个正方形,他有一只手。这是一个例子:

Example

ARM 上那个红色的东西是“手”,当我按下 shift 键时它会变成绿色。我像地面检查一样进行检测。这是代码:

void Update () {
touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);
mao1.SetBool("Ligado", ligado);

if (Input.GetKey(KeyCode.LeftShift)) {
ligado = true;
} else {
ligado = false;
}
}

touchDetect 工作正常,因为它在触摸框时变为“true”:

Example2

我的问题是:我不知道如何在脚本中输入我希望他捕获物体并在我想放下的时候放下。如果我需要使用 Raycast,代码会是什么样子?

最佳答案

为了统一“抓取”一个对象,只需将您希望抓取的游戏对象的 transform.parent 设置为您希望抓取的游戏对象的变换。

例如,在您的代码中,您使用的是 Physics2D.OverlapCircle,它返回一个 Collider2D 对象。您可以使用该碰撞器捕获您的游戏对象。

Collider2D touch = Physics2D.OverlapCircle(touchDetect.position, 0.01f, objectLayer);

if (Input.GetKey(KeyCode.LeftShift) && touch != null)
{
//grab on to the object
touch.gameObject.transform.parent = this.transform;

//if your box has a rigidbody on it,and you want to take direct control of it
//you will want to set the rigidbody iskinematic to true.
GetComponent<Rigidbody2D>().isKinematic = true;
}
else if( touch != null)
{
//let the object go
touch.gameObject.transform.parent = null;

//if your object has a rigidbody be sure to turn kinematic back to false
GetComponent<Rigidbody2D>().isKinematic = false;
}

关于c# - 抓取和放下物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34746926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com