【问题标题】:My object begins to spin when collides with the wall我的物体在与墙壁碰撞时开始旋转
【发布时间】:2016-09-23 21:28:15
【问题描述】:

我在 Unity 中有一个盒子,后面跟着一个飞机上的相机。我正在尝试处理盒子和不同物体之间的碰撞。当它与不同的东西碰撞时,它会旋转、跳跃并发生奇怪的事情。我向 YouTube 上传了一个视频来显示问题。 The video.

我创建了一个带有相机和盒子的 empty。这个空的刚体的质量为 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);
        }
    }
}

这是一个带有网格对撞机的飞机,有无刚体没有区别,同样的问题......


有什么帮助吗?

【问题讨论】:

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


    【解决方案1】:

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

    如果您移动一个对象并调整它的平移,那么当发生碰撞时,就好像该对象已经物化到引擎的另一个对象中,因为它将没有速度等移动,您会得到奇怪的行为。

    【讨论】:

    • 如果我有public Rigidbody rb,在Start()rb = GetComponent&lt;Rigidbody&gt; ();,我应该写Moving():rb.velocity = new Vector3(-deltaMovement, 0, 0);吗?当我在 if 语句中写下它时,它围绕 z 轴旋转。
    • 您是否删除或注释掉了所有其他代码?我刚刚对您描述的内容进行了快速测试,而我的立方体滑行没有问题?正如你在 start() 和我的 Update 方法中所说的: if (Input.GetKey("up")) { rb.velocity = new Vector3(1, 0, 0); }
    • 如果你强制一个速度,你也会消除你刚体上任何现有的速度,你可能不想这样做。即,当它试图翻滚/弹跳/下落时,如果您只是在对象上设置速度,您将破坏它的当前速度并可能破坏效果。在大多数情况下,施加力会产生最好的结果,当你想要特定的结果时,调整速度以作弊:)
    • 我有 if (Input.GetKey (KeyCode.A)) { rb.velocity = new Vector3(-1, 0, 0); } else if (Input.GetKey (KeyCode.D)){ rb.velocity = new Vector3 (1, 0, 0); }this is what happens 使用 A 和 D 键进行测试...非常奇怪,而不是滑行。
    • 我会接受你的回答,因为你解决了我的碰撞问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多