C# 保持速度的双向隐形传态

C# 保持速度的双向隐形传态,c#,unity3d,unity3d-2dtools,C#,Unity3d,Unity3d 2dtools,我有一个二维运动学的宇宙飞船,它正被刚体移动。MovePosition在它的fixeupdate()中。这艘船有四条光线从四个方向射出。每一条射线都会检查是否碰到力场。根据bool状态(可在鼠标向下移动时更改),船舶将移开或驶向战场 我还有一个双向传送系统,如果飞船与一个传送站相撞,它会出现在另一个传送站,反之亦然 我正在寻找的效果是,飞船从第二个传送站出来后,以相同的速度(进入第一个传送站时的速度)继续朝同一方向移动。我可以通过在飞船离开传送站时向它的变换添加一个向量来模拟这种效果,但是移动非

我有一个二维运动学的宇宙飞船,它正被
刚体移动。MovePosition
在它的
fixeupdate()
中。这艘船有四条光线从四个方向射出。每一条射线都会检查是否碰到力场。根据bool状态(可在鼠标向下移动时更改),船舶将移开或驶向战场

我还有一个双向传送系统,如果飞船与一个传送站相撞,它会出现在另一个传送站,反之亦然

我正在寻找的效果是,飞船从第二个传送站出来后,以相同的速度(进入第一个传送站时的速度)继续朝同一方向移动。我可以通过在飞船离开传送站时向它的
变换添加一个向量来模拟这种效果,但是移动非常迅速和突然(代码在下面注释)

这是附在两个传送带上的脚本-

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

public class teleport : MonoBehaviour {

    public Transform exitTeleport;
    static Transform thisTeleport;

    void OnTriggerEnter2D (Collider2D col)
    {
        if (col.CompareTag("ship"))
        {
            if (thisTeleport == exitTeleport)
            return;
            thisTeleport = this.transform;
            col.transform.position = exitTeleport.transform.position;
        }
    }

    void OnTriggerExit2D (Collider2D col)
    {
        if (col.CompareTag("ship"))
        {
            if (exitTeleport == thisTeleport)
            {
                thisTeleport = null;
            }
            //col.transform.position += new Vector3 (1, 0, 0);
        }
    }
}

你可以尝试存储和重新应用飞船进出传送机时的
刚体2d.velocity
,以保持动量

public Transform exitTeleport;
static Transform thisTeleport;
private Vector3 shipvelocity;

void OnTriggerEnter2D (Collider2D col)
{
    if (col.CompareTag("ship"))
    {
        if (thisTeleport == exitTeleport)
            return;
        thisTeleport = this.transform;
        shipvelocity = col.GetComponent<Rigidbody2D>().velocity;
        col.transform.position = exitTeleport.transform.position;
    }
}

void OnTriggerExit2D (Collider2D col)
{
    if (col.CompareTag("ship"))
    {
        if (exitTeleport == thisTeleport)
        {
            thisTeleport = null;
        }
        col.GetComponent<Rigidbody2D>().velocity = shipvelocity;
        //col.transform.position += new Vector3 (1, 0, 0);
    }
}
公共转换出口端口;
静态传输;
专用矢量3;速度;
无效OnTriggerEnter2D(碰撞的R2D列)
{
如果(列比较表(“船舶”))
{
如果(thisTeleport==退出端口)
返回;
thisTeleport=this.transform;
shipvelocity=col.GetComponent().velocity;
col.transform.position=exiteleport.transform.position;
}
}
无效OnTriggerExit2D(碰撞的R2D列)
{
如果(列比较表(“船舶”))
{
如果(exitTeleport==此远程端口)
{
thisTeleport=null;
}
col.GetComponent().velocity=shipvelocity;
//col.transform.position+=新向量3(1,0,0);
}
}

仍不工作。飞船传送,但不移动。此外,Update()中的Debug.Log(shipvelocity)始终显示(0.0,0.0),即使在其移动时也是如此。不知道为什么。谢谢你的回复。看起来我用的是刚体而不是刚体。我还将检索速度的语句上移了一行。尝试这些更改并让我知道。显然,rb.velocity/rb2d.velocity不适用于MovePosition,因此是(0,0)速度,也可能是这仍然不起作用的原因。这家伙发布了他的解决方案,但我不知道如何在我的脚本中使用它。是附加到ship的脚本,在fixedUpdate()中调用MovePosition。如果我能以某种方式得到船的速度,我相信你的解决方案会奏效。速度可以通过计算随时间移动的距离来确定。在到达传送机前将位置存储几帧,然后在到达传送机后找到位置,并计算两点之间经过了多少时间:
(curpos-prevpos)/deltatime
我不确定在身体处于运动状态时设置速度是可能的还是推荐的,但在决定使用移动位置的替代方案之前,值得一试。