团结中的立方体运动

时间:2018-04-02 16:24:49

标签: c# unity3d unityscript

我想修改以下脚本,仅在玩家滑动时改变方向。可能吗? (剧本在平地板上向各个方向滚动立方体)!!!!!

如下图所示:

enter image description here

        public float tumblingDuration = 0.2f;

        void Update ()
      {
        var dir = Vector3.zero;

        if (Input.GetKey(KeyCode.UpArrow))
            dir = Vector3.forward;
        if (Input.GetKey(KeyCode.DownArrow))
            dir = Vector3.back;
        if (Input.GetKey(KeyCode.LeftArrow))
            dir = Vector3.left;
        if (Input.GetKey(KeyCode.RightArrow))
            dir = Vector3.right;
        if (dir != Vector3.zero && !isTumbling)
        {
            StartCoroutine(Tumble(dir));
        }
    }
    bool isTumbling = false;
    IEnumerator Tumble(Vector3 direction)
    {
    isTumbling = true;
    var rotAxis = Vector3.Cross(Vector3.up, direction);
    var pivot = (transform.position + Vector3.down*0.5f ) +    direction*0.5f;
    var startRotation = transform.rotation;
    var endRotation   = Quaternion.AngleAxis(90.0f, rotAxis)*startRotation;
    var startPosition = transform.position;
    var endPosition   = transform.position + direction;
    var rotSpeed = 90.0f/tumblingDuration;
    var t = 0.0f;
    while ( t < tumblingDuration )
    {
        t += Time.deltaTime;
        transform.RotateAround(pivot, rotAxis, rotSpeed * Time.deltaTime);
        yield return null;
    }
    transform.rotation = endRotation;
    transform.position = endPosition;
    isTumbling = false;
    }

1 个答案:

答案 0 :(得分:1)

您应该使用Quaternion.Slerp

cube.transform.rotation = Quaternion.Slerp(cube.transform.rotation, cube.transform.rotation + dir * new Vector3(90, 0, 0), /*time*/ 2f);