C# 对象传送到另一个位置-Unity3D

C# 对象传送到另一个位置-Unity3D,c#,unity3d,C#,Unity3d,我正在开发一个俄罗斯方块游戏。我有一个问题。我刚刚实现了碎片的生成和自动翻滚。然而,当我去测试墙壁和地板上的碰撞时,对象正在传送到另一侧,如下面的gif所示 我使用了刚体和盒子碰撞器和OnTriggerEnter和OnTriggerExit。以检测碰撞,而不是穿过墙壁。在实现Spawn和其他工具之前,一切都正常工作。现在,物体在墙上碰撞时正在传送。我到底做了什么让这一切发生了?下面是实现前后的代码 实施前的代码: using System.Collections; using System.C

我正在开发一个俄罗斯方块游戏。我有一个问题。我刚刚实现了碎片的生成和自动翻滚。然而,当我去测试墙壁和地板上的碰撞时,对象正在传送到另一侧,如下面的gif所示

我使用了
刚体
盒子碰撞器
OnTriggerEnter
OnTriggerExit
。以检测碰撞,而不是穿过墙壁。在实现Spawn和其他工具之前,一切都正常工作。现在,物体在墙上碰撞时正在传送。我到底做了什么让这一切发生了?下面是实现前后的代码

实施前的代码:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;

[RequireComponent(typeof(Rigidbody))]
public class Movimentacao : MonoBehaviour
{

    public bool Rotation;
    public bool Rotation360;
    bool collidedRight = true;
    bool collidedLeft = true;
    bool collidedBottom = true;
    // Start is called before the first frame update
    void Start()
    {

    }

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

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {

           transform.position += new Vector3(0.6775f, 0, 0);

            if (!collidedRight) {
                transform.position += new Vector3(-0.6775f, 0, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {

           transform.position += new Vector3(-0.6775f, 0, 0);

            if (!collidedLeft){
             transform.position += new Vector3(0.6775f, 0, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {

           transform.position += new Vector3(0, -0.6775f, 0);

            if (!collidedBottom){
                transform.position += new Vector3(0, 0.6775f, 0);
            }

        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (Rotation)
            {
                if (!Rotation360)
                {
                    if (transform.rotation.z < 0)
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, 90);
                        } 
                    }
                    else
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, -90);
                        }
                    }
                }
                else
                {
                    if (collidedBottom)
                    {
                        transform.Rotate(0, 0, -90);
                    }

                }
            }
        }



    }

    void OnTriggerEnter(Collider other)
    {

        if (Mathf.Sign(other.transform.position.x) == 1)
        {
            collidedRight = false;
        }
        if (Mathf.Sign(other.transform.position.y) == -1)
        {
            collidedBottom = false;
        }
        if (Mathf.Sign(other.transform.position.x) == -1)
        {
            collidedLeft = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (!collidedRight)
        {
            collidedRight = true;
        }
        if (!collidedBottom)
        {
            collidedBottom = true;
        }
        if (!collidedLeft)
        {
            collidedLeft = true;
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;

[RequireComponent(typeof(Rigidbody))]
public class Movimentacao : MonoBehaviour
{

    public bool Rotation;
    public bool Rotation360;
    public float queda;
    public float velocidade;
    public float timer;
    bool collidedRight = true;
    bool collidedLeft = true;
    bool collidedBottom = true;
    // Start is called before the first frame update
    void Start()
    {
        timer = velocidade;
    }

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

        if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.DownArrow))
            timer = velocidade;

        if (Input.GetKey(KeyCode.RightArrow))
        {

            timer += Time.deltaTime;

            if (timer > velocidade)
            {
                transform.position += new Vector3(0.6775f, 0, 0);
                timer = 0;
            }

            if (!collidedRight) {
                transform.position += new Vector3(-0.6775f, 0, 0);
            }
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {

            timer += Time.deltaTime;

            if (timer > velocidade)
            {
                transform.position += new Vector3(-0.6775f, 0, 0);
                timer = 0;
            }

            if (!collidedLeft){
             transform.position += new Vector3(0.6775f, 0, 0);
            }
        }

        if (Input.GetKey(KeyCode.DownArrow))// || Time.time - queda >= 1)
        {

            timer += Time.deltaTime;

            if (timer > velocidade)
            {
                transform.position += new Vector3(0, -0.6775f, 0);
                timer = 0;
            }

            if (!collidedBottom){
                transform.position += new Vector3(0, 0.6775f, 0);
            }

            //queda = Time.time;

        }

        if (Time.time - queda >= 1 && !Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += new Vector3(0, -0.6775f, 0);

            if (!collidedBottom)
            {
                transform.position += new Vector3(0, 0.6775f, 0);
            }
            queda = Time.time;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (Rotation)
            {
                if (!Rotation360)
                {
                    if (transform.rotation.z < 0)
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, 90);
                        } 
                    }
                    else
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, -90);
                        }
                    }
                }
                else
                {
                    if (collidedBottom)
                    {
                        transform.Rotate(0, 0, -90);
                    }

                }
            }
        }



    }

    void OnTriggerEnter(Collider other)
    {

        if (Mathf.Sign(other.transform.position.x) == 1)
        {
            collidedRight = false;
        }
        if (Mathf.Sign(other.transform.position.y) == -1)
        {
            collidedBottom = false;
        }
        if (Mathf.Sign(other.transform.position.x) == -1)
        {
            collidedLeft = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (!collidedRight)
        {
            collidedRight = true;
        }
        if (!collidedBottom)
        {
            collidedBottom = true;
        }
        if (!collidedLeft)
        {
            collidedLeft = true;
        }
    }

}
使用系统集合;
使用System.Collections.Generic;
使用System.Collections.Specialized;
使用系统诊断;
使用System.Security.Cryptography;
使用系统线程;
使用UnityEngine;
使用Debug=UnityEngine.Debug;
[RequiredComponent(类型)(刚体))]
公共阶级运动:单一行为
{
公共图书馆轮换;
公共图书馆轮换360;
bool collidateright=true;
bool collidatelft=true;
bool collidedBottom=true;
//在第一帧更新之前调用Start
void Start()
{
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyDown(KeyCode.RightArrow))
{
transform.position+=新矢量3(0.6775f,0,0);
如果(!CollidatedRight){
transform.position+=新矢量3(-0.6775f,0,0);
}
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position+=新矢量3(-0.6775f,0,0);
如果(!collidatelft){
transform.position+=新矢量3(0.6775f,0,0);
}
}
if(Input.GetKeyDown(KeyCode.DownArrow))
{
transform.position+=新矢量3(0,-0.6775f,0);
如果(!CollidateBottom){
transform.position+=新矢量3(0,0.6775f,0);
}
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
如果(旋转)
{
如果(!旋转360)
{
if(变换旋转z<0)
{
如果(碰撞底部)
{
旋转(0,0,90);
} 
}
其他的
{
如果(碰撞底部)
{
旋转(0,0,-90);
}
}
}
其他的
{
如果(碰撞底部)
{
旋转(0,0,-90);
}
}
}
}
}
无效对撞机(对撞机其他)
{
if(数学符号(其他变换位置x)==1)
{
CollidateRight=false;
}
if(数学符号(其他变换位置y)=-1)
{
CollidateBottom=false;
}
if(数学符号(其他变换位置x)=-1)
{
collidatelft=false;
}
}
void OnTriggerExit(碰撞器其他)
{
如果(!CollidatedRight)
{
CollizedRight=true;
}
如果(!CollidateBottom)
{
CollidateBottom=true;
}
如果(!collidatelft)
{
CollizedLeft=真;
}
}
}
实施后的代码:

using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;

[RequireComponent(typeof(Rigidbody))]
public class Movimentacao : MonoBehaviour
{

    public bool Rotation;
    public bool Rotation360;
    bool collidedRight = true;
    bool collidedLeft = true;
    bool collidedBottom = true;
    // Start is called before the first frame update
    void Start()
    {

    }

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

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {

           transform.position += new Vector3(0.6775f, 0, 0);

            if (!collidedRight) {
                transform.position += new Vector3(-0.6775f, 0, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {

           transform.position += new Vector3(-0.6775f, 0, 0);

            if (!collidedLeft){
             transform.position += new Vector3(0.6775f, 0, 0);
            }
        }

        if (Input.GetKeyDown(KeyCode.DownArrow))
        {

           transform.position += new Vector3(0, -0.6775f, 0);

            if (!collidedBottom){
                transform.position += new Vector3(0, 0.6775f, 0);
            }

        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (Rotation)
            {
                if (!Rotation360)
                {
                    if (transform.rotation.z < 0)
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, 90);
                        } 
                    }
                    else
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, -90);
                        }
                    }
                }
                else
                {
                    if (collidedBottom)
                    {
                        transform.Rotate(0, 0, -90);
                    }

                }
            }
        }



    }

    void OnTriggerEnter(Collider other)
    {

        if (Mathf.Sign(other.transform.position.x) == 1)
        {
            collidedRight = false;
        }
        if (Mathf.Sign(other.transform.position.y) == -1)
        {
            collidedBottom = false;
        }
        if (Mathf.Sign(other.transform.position.x) == -1)
        {
            collidedLeft = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (!collidedRight)
        {
            collidedRight = true;
        }
        if (!collidedBottom)
        {
            collidedBottom = true;
        }
        if (!collidedLeft)
        {
            collidedLeft = true;
        }
    }

}
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
using Debug = UnityEngine.Debug;

[RequireComponent(typeof(Rigidbody))]
public class Movimentacao : MonoBehaviour
{

    public bool Rotation;
    public bool Rotation360;
    public float queda;
    public float velocidade;
    public float timer;
    bool collidedRight = true;
    bool collidedLeft = true;
    bool collidedBottom = true;
    // Start is called before the first frame update
    void Start()
    {
        timer = velocidade;
    }

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

        if (Input.GetKeyUp(KeyCode.RightArrow) || Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.DownArrow))
            timer = velocidade;

        if (Input.GetKey(KeyCode.RightArrow))
        {

            timer += Time.deltaTime;

            if (timer > velocidade)
            {
                transform.position += new Vector3(0.6775f, 0, 0);
                timer = 0;
            }

            if (!collidedRight) {
                transform.position += new Vector3(-0.6775f, 0, 0);
            }
        }

        if (Input.GetKey(KeyCode.LeftArrow))
        {

            timer += Time.deltaTime;

            if (timer > velocidade)
            {
                transform.position += new Vector3(-0.6775f, 0, 0);
                timer = 0;
            }

            if (!collidedLeft){
             transform.position += new Vector3(0.6775f, 0, 0);
            }
        }

        if (Input.GetKey(KeyCode.DownArrow))// || Time.time - queda >= 1)
        {

            timer += Time.deltaTime;

            if (timer > velocidade)
            {
                transform.position += new Vector3(0, -0.6775f, 0);
                timer = 0;
            }

            if (!collidedBottom){
                transform.position += new Vector3(0, 0.6775f, 0);
            }

            //queda = Time.time;

        }

        if (Time.time - queda >= 1 && !Input.GetKey(KeyCode.DownArrow))
        {
            transform.position += new Vector3(0, -0.6775f, 0);

            if (!collidedBottom)
            {
                transform.position += new Vector3(0, 0.6775f, 0);
            }
            queda = Time.time;
        }

        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            if (Rotation)
            {
                if (!Rotation360)
                {
                    if (transform.rotation.z < 0)
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, 90);
                        } 
                    }
                    else
                    {
                        if (collidedBottom)
                        {
                            transform.Rotate(0, 0, -90);
                        }
                    }
                }
                else
                {
                    if (collidedBottom)
                    {
                        transform.Rotate(0, 0, -90);
                    }

                }
            }
        }



    }

    void OnTriggerEnter(Collider other)
    {

        if (Mathf.Sign(other.transform.position.x) == 1)
        {
            collidedRight = false;
        }
        if (Mathf.Sign(other.transform.position.y) == -1)
        {
            collidedBottom = false;
        }
        if (Mathf.Sign(other.transform.position.x) == -1)
        {
            collidedLeft = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (!collidedRight)
        {
            collidedRight = true;
        }
        if (!collidedBottom)
        {
            collidedBottom = true;
        }
        if (!collidedLeft)
        {
            collidedLeft = true;
        }
    }

}
使用系统集合;
使用System.Collections.Generic;
使用System.Collections.Specialized;
使用系统诊断;
使用System.Security.Cryptography;
使用系统线程;
使用UnityEngine;
使用Debug=UnityEngine.Debug;
[RequiredComponent(类型)(刚体))]
公共阶级运动:单一行为
{
公共图书馆轮换;
公共图书馆轮换360;
公共浮球;
公共浮式杀鼠器;
公共浮动计时器;
bool collidateright=true;
bool collidatelft=true;
bool collidedBottom=true;
//在第一帧更新之前调用Start
void Start()
{
定时器=杀速度;
}
//每帧调用一次更新
无效更新()
{
if(Input.GetKeyUp(KeyCode.RightArrow)| | Input.GetKeyUp(KeyCode.LeftArrow)| | Input.GetKeyUp(KeyCode.DownArrow))
定时器=杀速度;
if(Input.GetKey(KeyCode.RightArrow))
{
timer+=Time.deltaTime;
如果(定时器>速度控制器)
{
transform.position+=新矢量3(0.6775f,0,0);
定时器=0;
}
如果(!CollidatedRight){
transform.position+=新矢量3(-0.6775f,0,0);
}
}
if(Input.GetKey(KeyCode.LeftArrow))
{
timer+=Time.deltaTime;
如果(定时器>速度控制器)
{
transform.position+=新矢量3(-0.6775f,0,0);
定时器=0;
}
如果(!collidatelft){
transform.position+=新矢量3(0.6775f,0,0);
}
}
if(Input.GetKey(KeyCode.DownArrow))//| | Time.Time-queda>=1)
{
timer+=Time.deltaTime;
如果(定时器>速度控制器)
{
transform.position+=新矢量3(0,-0.6775f,0);
定时器=0;
}
如果(!CollidateBottom){
transform.position+=新矢量3(0,0.6775f,0);
}
//奎达=时间。时间;
}
if(Time.Time-queda>=1&&!Input.GetKey(KeyCode.DownArrow))
{
transform.position+=新矢量3(0,-0.6775f,0);
如果(!CollidateBottom)
{
使改变