亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

游戲重置時,Hinge Joint 2D 停止正常工作

游戲重置時,Hinge Joint 2D 停止正常工作

C#
慕碼人2483693 2023-04-29 16:34:21
我在使用一個簡單的 hingejoint2d 時遇到了麻煩,它由一個簡單的鏈條組成,上面附有一個盒子。它工作正常,直到游戲重新啟動,當游戲重新啟動時,盒子從鏈條上掉下來,但鏈條繼續在原處。我不知道是什么原因造成的。這是重置對象的代碼。    private Quaternion startRot;    private Vector3 startLocalScale;    private Rigidbody2D rb;    private HingeJoint2D hj;    // Start is called before the first frame update    void Start()    {        startPos = transform.position;        startRot = transform.rotation;        startLocalScale = transform.localScale;        hj = GetComponent<HingeJoint2D>();        if (GetComponent<Rigidbody2D>() != null)        {            rb = GetComponent<Rigidbody2D>();        }    }    // Update is called once per frame    void Update()    {    }    public void ResetObjects()    {        transform.position = startPos;        transform.rotation = startRot;        transform.localScale = startLocalScale;        if (hj != null)            return;        if (rb != null)        {            rb.velocity = Vector3.zero;        }    }```
查看完整描述

1 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

您不應該直接訪問對象變換,如果您使用的是剛體,請讓剛體組件完成工作!否則事情往往會變得有點混亂。

我今天遇到了這個問題,我使用關節限制來解決這個問題。

http://img3.sycdn.imooc.com/644cd6a500015cb806370286.jpg

啟用使用限制。然后您可以根據需要編輯約束。


using UnityEngine;


? ? public class Example : MonoBehaviour

? ? {

? ? ? ? void Start()

? ? ? ? {

? ? ? ? ? ? // Set the hinge limits for a door.

? ? ? ? ? ? HingeJoint hinge = GetComponent<HingeJoint>();


? ? ? ? ? ? JointLimits limits = hinge.limits;

? ? ? ? ? ? limits.min = 0;

? ? ? ? ? ? limits.bounciness = 0;

? ? ? ? ? ? limits.bounceMinVelocity = 0;

? ? ? ? ? ? limits.max = 90;

? ? ? ? ? ? hinge.limits = limits;

? ? ? ? ? ? hinge.useLimits = true;

? ? ? ? }

? ? }

查看完整回答
反對 回復 2023-04-29
  • 1 回答
  • 0 關注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號