Unity只能以特定角度和位置传送另一个游戏对象

春阴蛋

我正在尝试创建门户,但是我不明白为什么要使用代码:

playerControl.transform.position = new Vector3(destination.position.x, destination.position.y, destination.position.z);

仅适用于某些位置和角度。

这是代码的设置

    public class Teleport : Interactable
{
    public Transform destination;
    private GameObject playerControl;
    private PortalAnimationController portalController;

    // Start is called before the first frame update
    void Start()
    {//get the player information
        playerControl = PlayerManager.instance.player;
        portalController = PortalAnimationController.instance;
    }


    public override void interact()
    {
        //base.interact();
        //run the teleport method

        StartCoroutine("teleport");

        //Debug.Log(playerControl.GetInstanceID());

    }


    /*
     1. trigger the panel
     2. stop the time
     3. teleport the player
     */
    private IEnumerator teleport()
    {
        Time.timeScale = 0f;//stop the time
        portalController.fadeOut();
        //wait for seconds realtime can avoid stopping when timescale is 0
        yield return new WaitForSecondsRealtime(portalController.duration);//wait for the transition
        Debug.Log("wait finish");
        //after the the wait
        Debug.Log(playerControl.transform.position + "before");
        playerControl.transform.position = new Vector3(destination.position.x, destination.position.y, destination.position.z);
        if(playerControl.transform.position!= destination.position)
        {
            Debug.Log(playerControl.transform.position);
        }
        else
        {
            Debug.Log(playerControl.transform.position+"pass");
        }
        portalController.fadeOut();
        Time.timeScale = 1f;
    }
}

可交互类仅包含一个“公共虚拟void interact()”方法,以使传送能够覆盖它。触发方法是通过以下代码:

void Update()
    {
        RaycastHit hit;
        if (Input.GetButtonDown("Interact"))
        {
            if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, interactableRange))
            {
                Interactable interactable = hit.collider.GetComponent<Interactable>();
                //if the player is looking at the interactable
                //Debug.Log("Interact with " + hit.transform.name);
                if (interactable != null)
                {
                    //highlight the object
                    interactable.interact();
                    //if player press the interact key, then pick it up
                    Debug.Log("Interact with " + hit.transform.name);


                }
            }
        }
    }

当按下e键时,它将发出光线,然后击中对撞机框以检索可交互组件并立即调用interact()。

我已经对其进行了数百次测试,只有3个结果。1.玩家实际上被传送了。(成功)2.玩家以1帧的速度闪烁到目的地,然后又回到了原来的位置。(失败)3.玩家根本没有移动。(失败)

影片连结

春阴蛋

谢谢所有尝试提供帮助的人。我终于找到问题所在。之所以不能进行传送,是因为我的播放器具有一个称为“角色控制器”的组件,该组件将覆盖播放器的更改位置,并将其发送回原处。因此,在播放器回到原始位置之前,我会将其传送到正确的位置一帧。

我的解决方法很简单:

characterController.enable = false;
playerControl.transform.position = new Vector3(destination.position.x, destination.position.y, destination.position.z);
characterController.enable = true;

这样,字符控制器将不会被调用,因此传送将正常运行。

在此处输入图片说明

再次感谢所有尝试提供帮助的人。:D

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Unity/C# - 基于另一个对象手动设置对象的位置和旋转

Unity,OnMouseOver被前面有对撞机的另一个游戏对象阻挡

Unity 5.6-C#-使用AddComponent <T>将游戏对象添加到另一个游戏对象

如何在另一个游戏对象Unity3D的前移方向上移动游戏对象

unity如何相对于某个角度从另一个对象“反弹”一个对象?

映射对象数组并在特定位置显示另一个元素

在 Unity 中,如何根据另一个脚本中的随机选择动态修改游戏对象标签?

旋转远离另一个游戏对象的朝向的游戏对象

如何使游戏对象仅在 x 轴上跟随另一个游戏对象?

从一个位置实例化尽可能多的游戏对象,并在另一个位置结束

在运行时将游戏对象绑定到Unity 3D中的另一个游戏对象

如何从特定的另一个对象创建对象

如何在激活一个游戏对象并停用另一个游戏对象的同时从另一个场景加载场景?

如何在Unity3d中将C#组件从一个游戏对象传递到另一个

从另一个文件中grep一个文件只能以一种方式工作。为什么?

如何通过GetComponent从另一个游戏对象中的另一个脚本访问变量?

在Unity C#中使一个对象从另一个对象中消失

如何将一个特定的预制件/游戏对象及其所有依赖项复制到另一个项目?

如何在Unity中另一个GameObject的变换位置实例化对象?

将前X行传送到一个命令,其余传送到另一个命令

如何确保特定的类只能创建另一个类的实例?

将特定属性从一个对象移动到另一个对象

如何将一个FLWOR传送到另一个?

如何将一个Zip传送到另一个Zip

使用C#在Unity中将另一个对象作为中心时查找两个对象之间的角度

如何测试一个对象与另一个 Unity 的距离

如何拥有一个只能由另一个特定类实例化的类

从特定位置获取文件内容到另一个特定位置

递归过滤一个对象和另一个对象