简体   繁体   English

Unity3D矢量3传送

[英]Unity3D Vector 3 Teleporting

image 图片

Hey Folks! 嘿伙计!

I attached an image to visualize my explanation of the problem i have. 我附上一张图片以形象化地说明我所遇到的问题。 In my game, there is a green ball rolling along an endless road, getting chased by a yellow ball. 在我的游戏中,有一个绿色的球沿着无尽的道路滚动,被一个黄色的球追赶。 To make the road seem endless, the ball gets teleported back (purple line on the bottom image), when it crosses a certain distance on the x-axis (purple line on the top image). 为了使道路看起来无止境,当球在x轴上交叉一定距离(顶部图像上的紫色线)时,球会向后传送(底部图像上的紫色线)。 I did it like this: 我这样做是这样的:

if (gameObject.transform.position.x <= -20) {
    gameObject.transform.position = new Vector3 (transform.position.x + 80, transform.position.y, transform.position.z);
}

and it works totally fine. 而且效果很好。

When the green ball is getting „teleported“, the yellow ball should get teleported back with the same value, so the distance between the too balls doesn't change after teleporting. 当绿色球“传送”时,黄色球应以相同的值向后传送,因此,太球之间的距离在传送后不会改变。 So I made a public game object „enemy“, placed the yellow ball inside the enemy field in the inspector and changed the code to this: 因此,我制作了一个公共游戏对象“敌人”,将黄球放置在检查员的敌方区域内,并将代码更改为:

if (gameObject.transform.position.x <= -20) {
    gameObject.transform.position = new Vector3 (transform.position.x + 80, transform.position.y, transform.position.z);
    enemy.transform.position = new Vector3 (transform.position.x + 80, transform.position.y, transform.position.z);
}

But as you can see on the bottom image, the yellow ball wasn't teleported on the same distance. 但是,如您在底部图像上所看到的,黄色球并没有以相同的距离传送。 It seems like the distance is added from it's original starting point and not from the point it had before teleporting. 似乎距离是从原始起点算起的,而不是从传送前的距离算起的。 Hope you guys know a solution for this. 希望你们知道解决方案。

The Yellow Ball has a script to follow the Player, maybe it has to do something with this? 黄球有一个跟随玩家的脚本,也许它需要为此做些事情?

void Update ()
{

    //enemy follows the ball with speed 8
    transform.LookAt (target);
    transform.Translate (Vector3.forward * 8 * Time.deltaTime);

    //fix y-position
    Vector3 tmp = transform.position;
    tmp.y = lockedY;
    transform.position = tmp;

}

You want to move the enemy back 80 units, then you need to move the enemy back 80 units. 您需要将敌人移回80个单位,然后需要将敌人移回80个单位。

enemy.transform.position = new Vector3( enemy .position.x + 80, enemy .position.y, enemy .position.z); 敌人。 .position.x + 80, enemy.transform.position = new Vector3( 敌人。位置 .position.x + 80, 敌人位置 .position.y, 敌人位置 .position.z);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM