Unity禁用子旋转

问题描述 投票:0回答:1

我有一个问题在团结2d与我的对象,当我拍摄箭头到框对撞机到另一个元素,当它击中并进入孩子我的意思是箭头成为父母的孩子(BOX)孩子开始旋转到左边和右边..我真的想要禁用孩子的左右旋转..盒子(父)仍然需要像之前一样旋转..我有这样的代码,我的箭头在rigidbody2d上是运动模式......

这是箭头的脚本..

{
    public float flySpeed = 20f;
    private Rigidbody2D arrowBody;
    private bool shouldFly;


    // Start is called before the first frame update
    void Start()
    {
        shouldFly = true;
        arrowBody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if (shouldFly == true)
        {
            //make our pin fly
            arrowBody.MovePosition(arrowBody.position + Vector2.up * flySpeed * Time.deltaTime);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "target")
        {

            shouldFly = false;
            transform.SetParent(collision.gameObject.transform);


        } else if(collision.tag == "arrow")
        {

            SceneManager.LoadScene("quickGameOverScene");
        }
    }
}
c# visual-studio unity3d unify
1个回答
0
投票

我真的很困惑你想做什么。我不明白你是否要冻结旋转或移动,所以我会发布两者的答案。为了防止父对象引起的翻译和旋转,你可以像这样使用LateUpdate

Quaternion InitRot;
Vector3 InitPos;

void Start () {
    InitRot = transform.rotation;
    InitPos = transform.position;
}
void Update()
{
    //figuring out when to save position when attached to BOX
    if(gameObject.transform.parent == null)
    {
        InitRot = transform.rotation;
        InitPos = transform.position;
    }
}
void LateUpdate () {
    //If attached to box do not translate do not rotate
    if (gameObject.transform.parent != null)
    {
        transform.rotation = InitRot;
        transform.position = InitPos;
    }
}

您可以从here获得有关此解决方案的更多信息

编辑因为上面的答案没有为OP工作。我弄清楚实际问题是什么。 OP的代码非常适合移动箭头,但问题很可能是他旋转盒子的地方。

如果他使用transform.Rotate(Vector3.forward* 90)旋转盒子,由于每个Update中的帧时间不相同,每个Update都会有相同旋转量引起的失真。因此,他需要使用Time.deltaTime旋转框以进行一致的旋转,如下所示:transform.Rotate(Vector3.forward* 90*Time.deltaTime);这会在每个时间间隔内旋转相同的量并消除失真。这些是我用于任务的脚本,它适用于我。

箭头脚本:

public float flySpeed = 20f;
private Rigidbody2D arrowBody;
private bool shouldFly;
private Vector2 initPos;
private Quaternion initRot;
// Start is called before the first frame update
void Start()
{
    shouldFly = true;
    arrowBody = GetComponent<Rigidbody2D>();
    //arrowBody.isKinematic = true;
    initPos = gameObject.transform.position;
    initRot = gameObject.transform.rotation;
}

// Update is called once per frame
void Update()
{
    if (shouldFly == true)
    {         
        //make our pin fly
        arrowBody.MovePosition(arrowBody.position + Vector2.up * flySpeed * Time.deltaTime);
    }
    if(gameObject.transform.parent == null)
    {
        initPos = gameObject.transform.position;
        initRot = gameObject.transform.rotation;
    }

}
void LateUpdate()
{
    if (gameObject.transform.parent != null)
    {
        gameObject.transform.position = initPos;
        gameObject.transform.rotation = initRot;
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
    Debug.Log("Collision happened");
    if (collision.tag == "target")
    {          
        shouldFly = false;          
        transform.SetParent(collision.gameObject.transform);
    }
    else if (collision.tag == "arrow")
    {

        SceneManager.LoadScene("quickGameOverScene");
    }
}

和旋转框的脚本:

public float rotationSpeed = 70f;
void Update()
{
    transform.Rotate(Vector3.back, rotationSpeed * Time.deltaTime);
}
© www.soinside.com 2019 - 2024. All rights reserved.