Unity3d Unity只能在特定角度和位置传送另一个游戏对象
我正在尝试创建一个门户,但我不明白为什么代码:Unity3d Unity只能在特定角度和位置传送另一个游戏对象,unity3d,Unity3d,我正在尝试创建一个门户,但我不明白为什么代码: playerControl.transform.position = new Vector3(destination.position.x, destination.position.y, destination.position.z); 仅适用于某些位置和角度 这是代码的设置 public class Teleport : Interactable { public Transform destination; priv
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;
}
}
可交互类
仅包含一个“public virtual void interactive()”方法,以便远程传送覆盖它。
触发的方式是通过以下代码:
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);
}
}
}
}
void Update()
{
雷卡斯特击中;
if(Input.GetButtonDown(“交互”))
{
if(物理光线投射(cam.transform.position、cam.transform.forward、out-hit、interactiablerange))
{
Interactitable Interactitable=hit.collider.GetComponent();
//如果玩家正在查看可交互的
//Log(“与“+hit.transform.name”交互);
if(可交互!=null)
{
//突出显示对象
可交互的;
//如果玩家按下互动键,则将其拾取
Log(“与“+hit.transform.name”交互);
}
}
}
}
当按下e键时,它会发射一条射线,然后点击碰撞器框以检索可交互组件并立即调用interact()
我已经测试过几百次了,结果只有三个。
1.玩家实际上被远程传送。(成功)
2.播放器在1帧内闪烁到目的地,然后返回到原来的位置。(失败)
3.玩家完全不移动。(失败)
谢谢所有试图帮助你的人。我终于找到了问题所在。传送失败的原因是因为我的玩家有一个叫做角色控制器的组件,这个组件会覆盖玩家改变的位置,并将其发送回原来的位置。所以,我会让玩家在回到原来的位置之前被传送到正确的位置一帧 我的解决方法是简单地执行以下操作:
characterController.enable = false;
playerControl.transform.position = new Vector3(destination.position.x, destination.position.y, destination.position.z);
characterController.enable = true;
这样,角色控制器将不会被调用,因此传送将正常工作
再次感谢所有试图帮助的人谢谢所有想帮忙的人。我终于找到了问题所在。传送失败的原因是因为我的玩家有一个叫做角色控制器的组件,这个组件会覆盖玩家改变的位置,并将其发送回原来的位置。所以,我会让玩家在回到原来的位置之前被传送到正确的位置一帧 我的解决方法是简单地执行以下操作:
characterController.enable = false;
playerControl.transform.position = new Vector3(destination.position.x, destination.position.y, destination.position.z);
characterController.enable = true;
这样,角色控制器将不会被调用,因此传送将正常工作
再次感谢所有试图帮助的人D只是一个猜测。如果你没有将时间刻度设置为0,你会有同样的问题吗?@Mykhailo Khadzhynov是的,即使我没有将时间刻度设置为0,我仍然会有同样的问题。我猜你会有两次一次的传送,如果你有两个方向的传送机,这是可能发生的。如果是这样,试着添加一些简单的逻辑,这将允许您只进行一次远程传送,比如
If(Input.GetButtonDown(“Interact”)&&canTeleport){canTeleport=false
,当然,当你直接看传送机时,只需按一次交互按钮。另外,检查一下,它是否在单个帧中工作,没有协同程序。看起来你的玩家位置上发生了一些与执行顺序相关的交互。你是如何进行玩家移动的?只是一个猜测。你有正确的方法吗同样的问题,如果你没有将时间刻度设置为0?@Mykhailo Khadzhynov是的,我仍然有同样的问题,即使我没有将时间刻度设置为0,我猜你有一个接一个的两个远程传送-如果你有两个方向的远程传送,这是可能发生的。如果是这样,试着添加一些简单的逻辑,这将允许你只进行一次远程传送,likeif(Input.GetButtonDown(“交互”)和&canTeleport){canTeleport=false
,当然,当你直接看传送机时,只需按一次“交互”按钮。另外,请检查,它是否在单个帧中工作,没有协同程序。看起来你的玩家位置上发生了一些与执行顺序相关的交互。你是如何进行玩家移动的?