【问题标题】:how to get my clawhand to grab my objectholder?如何让我的爪手抓住我的对象持有人?
【发布时间】:2022-09-24 09:26:20
【问题描述】:

[![大家好,我需要帮助!我需要我的爪手能够抓住我的物体支架(红色球旁边的橙色东西。我的物体支架已经有一个弹簧接头所以我希望我的爪手能够抓住它并用球拉它。当我的手打开然后它射击。但是当我试图抓住它时,我的手直接穿过。 我的爪手有刚体,盒子碰撞器,配置关节,并且是这段代码的动画

public class open2close : MonoBehaviour
{
    public float speed;
    private Animation anim;
    Rigidbody rb;
   

    void Start()
    {
        anim = gameObject.GetComponent<Animation>();
        rb = GetComponent<Rigidbody>();
        
    }


    void Update()
    {
        //********************Open pincher ********************
        if (Input.GetKey(KeyCode.X))
        {
            anim.Play(\"clawopen\");
        
            
        }
        //*******************Close pincher ********************
        if (Input.GetKey(KeyCode.Y))
        {
            anim.Play(\"clawclose\");
           
        }


    }
}

至于我的对象持有人,它有盒子对撞机、弹簧接头、刚体和旋转约束。有人可以指导我或帮助我做什么,谢谢。

    标签: unity3d


    【解决方案1】:

    爪手上最好不要有对撞机。这是因为您将刚体与动画混合在一起,这可能会变得混乱。我建议保留所有东西,除了爪手上的盒子对撞机。将其设置为触发器.您可以在 if 语句中放置布尔值来打开和关闭爪。这样,您可以将对象保持器的位置设置为爪(如果它们正在接触)。

    将您的脚本更改为类似

    public class open2close : MonoBehaviour
    {
        public float speed;
        private Animation anim;
        [SerializeField] bool isClosed;
        Rigidbody rb;
    
        void Start()
        {
            anim = gameObject.GetComponent<Animation>();
            rb = GetComponent<Rigidbody>();
        }
        void Update()
        {
            if (Input.GetKey(KeyCode.X))
            {
                anim.Play("clawopen");
                isClosed = false;
            }
            if (Input.GetKey(KeyCode.Y))
            {
                anim.Play("clawclose");
                isClosed = true;
            }
        }
        void OnTriggerStay(Collider obj)
        {
            Rigidbody colRb = obj.attachedRigidbody;
            if (colRb != null && isClosed)
            {
                colRb.position = transform.position;
            }
        }
    }
    

    让我知道这是否有效并具体!谢谢 :)

    【讨论】:

    • 好的,所以我现在正在尝试,但它在 GetCurrentAnimatorStateInfo 上给了我一个错误
    • 另外,如果我摆脱了盒子对撞机,我该如何触发它?
    • @alexa您可以删除GetCurrentStateInfo,只需在按下y时将isClosed设置为true。
    • @alexa 我的意思是在 box collider 组件中将 isTrigger 设置为 true
    • 好的,我会加入。
    猜你喜欢
    • 2018-07-31
    • 2017-07-29
    • 2020-08-18
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多