【问题标题】:Test what face of a cuboid is in contact with the floor测试长方体的哪个面与地板接触
【发布时间】:2015-11-13 23:10:56
【问题描述】:

我目前正在开发一款游戏,我必须使用箭头键移动长方体,同时牢记棋盘的限制。

物体只能一步一步移动,意思是:

  • 如果它向上并且您按任意键:它将朝那个方向下降并且 把自己放在一边。
  • 如果它在它的一侧:它将移动 1 步 那个方向。

我有一个脚本可以做到这一点……对于一个立方体。但是,我无法让它与长方体一起使用。

我想我应该检查哪张脸实际上与地板接触,然后根据结果做不同的动作。

  • 如果 2 个方形面之一与地板接触,我应该 做我的脚本目前做的事情。
  • 如果 4 个矩形之一 脸与地板接触,我应该调整我的脚本以移动 同样的方法。

这似乎是一个好的解决方案?如果是这样,你能帮我弄清楚我必须写什么条件来检查当前与地板接触的脸吗?

我的 UnityScript(我打算将其转换为 C#)

var rotator : Transform;
var speed = 1.0;
var halfSize = 0.5;
private var rotating = false;

function RotateCube(refPoint : Vector3, rotationAxis : Vector3) {
    rotator.localRotation = Quaternion.identity;
    rotator.position = transform.position -Vector3.up*halfSize + refPoint;
    transform.parent = rotator;
    var angle : float = 0;

    while(angle < 90.0) {
        angle += Time.deltaTime*90.0*speed;
        rotator.rotation = Quaternion.AngleAxis(Mathf.Min(angle,90.0),rotationAxis);
        yield;
    }

    transform.parent = null;
    rotating = false;
}

function Start() {
    rotator = (new GameObject("Rotator")).transform;
}

function Update () {
    if (!rotating) {
        if (Input.GetKey("right")) {
            rotating = true;
            RotateCube(Vector3.right*halfSize,-Vector3.forward);
        }
        else if (Input.GetKey("left")) {
            rotating = true;
            RotateCube(-Vector3.right*halfSize,Vector3.forward);
        }
        else if (Input.GetKey("up")) {
            rotating = true;
            RotateCube(Vector3.forward*halfSize,Vector3.right);
        }
        else if (Input.GetKey("down")) {
            rotating = true;
            RotateCube(-Vector3.forward*halfSize,-Vector3.right);
        }
    }
}

【问题讨论】:

    标签: c# unity3d unityscript


    【解决方案1】:

    你应该使用

    function OnCollisionEnter(Collision : collision)
    

    检测任何碰撞。如果您想检测每一帧中的碰撞,请使用

    function OnCollisionStay(Collision : collision)
    

    查看他们的描述 herehere

    然后使用collision.contacts检测接触点(description)。

    这是一个例子:

    function OnCollisionStay(collision : Collision) {
        for (var contact : ContactPoint in collision.contacts) {
            print(contact.thisCollider.name + " hit " + contact.otherCollider.name);
            // Visualize the contact point
            Debug.DrawRay(contact.point, contact.normal, Color.white);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      相关资源
      最近更新 更多