当与墙碰撞时,我的物体开始旋转

时间:2016-09-23 21:28:15

标签: c# unity3d collision-detection gameobject rigid-bodies

我在Unity中有一个盒子,后面跟着一架飞机上的摄像头。我试图处理盒子和不同物体之间的碰撞。当它与它旋转的不同事物碰撞时,跳跃并发生奇怪的事情。我上传了一段视频来展示问题。 The video

我创建了一个带有相机和盒子的。这个空的质量为1的刚体

空有一个脚本组件:

using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }
    void OnCollisionEnter(Collision collision)
    {
        Debug.Log ("Entered OnCollisionEnter function");
        if (collision.gameObject.name == "Wall") {
            GetComponent<Rigidbody>().velocity = Vector3.zero;
            Debug.Log ("Inside if statement");
        }

    }
}

正如你所看到的,我试图处理碰撞,编写一个停止移动立方体的代码。

可以帮助你们的其他信息:

方框

它有一个盒子对撞机。脚本:

using UnityEngine;
using System.Collections;

public class MoveCharacter : MonoBehaviour {

    public float deltaMovement = 10f;

    // Use this for initialization
    void Start ()
    {
    }

    // Update is called once per frame
    void Update ()
    {
        Moving();
    }


    void Moving()
    {
        //Moves the character to where it needs.
        if (Input.GetKey (KeyCode.A)) {
            transform.Translate (new Vector3 (-deltaMovement, 0f, 0f) * Time.deltaTime);
        } else if (Input.GetKey (KeyCode.D)){
            transform.Translate (new Vector3 (deltaMovement, 0f, 0f) * Time.deltaTime);
        }
        float yRotation = Camera.main.transform.eulerAngles.y;
        float movementX = Mathf.Sin ((yRotation * Mathf.PI) / 180) * deltaMovement;
        float movementZ = Mathf.Cos ((yRotation * Mathf.PI) / 180) * deltaMovement;

        if (Input.GetKey (KeyCode.W)) {
            transform.Translate (new Vector3 (movementX, 0f, movementZ) * Time.deltaTime, Space.World);
        } else if (Input.GetKey (KeyCode.S)){
            transform.Translate (new Vector3 (-movementX, 0f, -movementZ) * Time.deltaTime, Space.World);
        }
    }
}

这是一架带有网状对撞机的飞机,有或没有刚体并没有什么区别,同样的问题......

请帮忙吗?

1 个答案:

答案 0 :(得分:2)

如果你正在使用物理学,你不应该移动一个物体我改变它的平移,你应该通过对物体施加力来移动它,或者至少为它添加一个速度。这将允许物理引擎正确计算与其他刚体的反应。

如果你移动一个物体我调整它的平移然后当发生碰撞时,就好像物体已经物化到另一个物体到发动机,因为它将在没有速度等情况下移动,你会得到奇怪的行为。

相关问题