为什么我的僵硬的身体不会停止碰撞?

时间:2018-02-15 21:21:03

标签: c# unity3d collision-detection

我是Unity的新手。

下面是我的简单字符控制器C#脚本。我使用三维立方体作为我的墙壁和玩家的箱子对撞机和刚体。目前,当我的玩家接触到墙壁时,它就会继续前进。

为什么我的脚本无效?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Controller : MonoBehaviour {

    public float speed = 180;

    private Rigidbody rig;

    private Vector3 movement;
    // Use this for initialization
    void Start () {

        rig = GetComponent<Rigidbody>();
    }

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

        if (rig.velocity.magnitude <= 0)
        {
            if (Input.GetKeyUp("up"))
                rig.velocity = new Vector3(0, 0, rig.position.z * speed );            
            else if (Input.GetKeyUp("down"))
                rig.velocity = new Vector3(0, 0, rig.position.z * speed * -1);
            else if (Input.GetKeyUp("right"))
                rig.velocity = new Vector3(rig.position.x * speed, 0, 0);
            else if (Input.GetKeyUp("left"))
                rig.velocity = new Vector3(rig.position.x * speed * -1, 0, 0);
        }
    }

    void OnCollisionEnter(Collision collision)
    {
            rig.velocity = Vector3.zero;
    }
}

1 个答案:

答案 0 :(得分:4)

上面的剧本有效...我的位置高于我的墙壁的位置所以从来没有任何碰撞。我感到愚蠢。留下帖子提醒我失败。