简体   繁体   English

如何对所有对象使用循环传送以及随机传送?

[英]How can I use loop for all objects to teleport and also for random?

I tried first to use the loop flag on the line:我首先尝试在行上使用循环标志:

if (currentlyTeleportedObjects[i].teleportOrder >= teleporters.Count - 1 && isLoop == true)

IF the loop flag is false then I'm getting exception error on line 46:如果循环标志为假,那么我在第 46 行收到异常错误:

GameObject destinationTeleporter = teleporters[currentlyTeleportedObjects[i].teleportOrder];

If the loop flag is false it should make one round for all objects between the waypoints.如果循环标志为假,它应该为路点之间的所有对象进行一轮。 If the random flag is true it should move each object to teleport randomly between the waypoints.如果随机标志为真,它应该移动每个 object 以在航路点之间随机传送。

The script:剧本:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.WebSockets;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;

public class Teleporting : MonoBehaviour
{
    public List<ObjectToTeleport> objectsToTeleport; //list of all posible objects to be teleported
    public List<ObjectToTeleport> currentlyTeleportedObjects;   //list of objects that are currently being teleported at least once
    public List<GameObject> teleporters;    //teleports positions
    public GameObject[] groups;
    public bool isLoop = false;
    public bool isRandom = false;
    public bool isInGroups = false;

    public int teleportationsCount = 0; //the number of teleportations have been made

    [Serializable]
    public class ObjectToTeleport
    {
        public GameObject teleportableObject;
        public int teleportOrder;
    }

    public void Start()
    {
        if (teleporters.Count > 1 && objectsToTeleport.Count > 0)
        {
            //at the start and every 3 seconds call "MoveTeleportableObjects" method
            InvokeRepeating("MoveTeleportableObjects", 0, 3f);
        }
    }

    private void MoveTeleportableObjects()
    {
        //Add new object to teleport queue
        if (teleportationsCount < objectsToTeleport.Count)
            currentlyTeleportedObjects.Add(objectsToTeleport[teleportationsCount]);

        //move objects on the teleport queue
        for (int i = 0; i < currentlyTeleportedObjects.Count; i++)
        {
            //Get correspondent teleporter            
            GameObject destinationTeleporter = teleporters[currentlyTeleportedObjects[i].teleportOrder];
            //set position of the item to correspondent teleporter
            currentlyTeleportedObjects[i].teleportableObject.transform.position = destinationTeleporter.transform.position;
            //update teleporter order so next time goes to the next teleporter
            if (currentlyTeleportedObjects[i].teleportOrder >= teleporters.Count - 1 && isLoop == true)
            {
                currentlyTeleportedObjects[i].teleportOrder = 0;
            }
            else
            {
                currentlyTeleportedObjects[i].teleportOrder++;
            }
        }
        teleportationsCount++;
    }
}

Here we go again Yasmin ^^在这里我们再次 go Yasmin ^^

Code could be simplified, but I hope this time will be easyiest to understand:代码可以简化,但我希望这次最容易理解:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.WebSockets;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;

public class SOTest : MonoBehaviour
{
    public List<ObjectToTeleport> objectsToTeleport; //list of all posible objects to be teleported
    public List<ObjectToTeleport> currentlyTeleportedObjects;   //list of objects that are currently being teleported at least once
    public List<GameObject> teleporters;    //teleports positions
    public GameObject[] groups;
    public bool isLoop = false;
    public bool isRandom = false;
    public bool isInGroups = false;

    public int teleportationsCount = 0; //the number of teleportations have been made

    [Serializable]
    public class ObjectToTeleport
    {
        public GameObject teleportableObject;
        public int teleportOrder;
    }

    public void Start()
    {
        if (teleporters.Count > 1 && objectsToTeleport.Count > 0)
        {
            //at the start and every 3 seconds call "MoveTeleportableObjects" method
            InvokeRepeating("MoveTeleportableObjects", 0, 1f);
        }
    }

    private void MoveTeleportableObjects()
    {
        //Add new object to teleport queue
        if (teleportationsCount < objectsToTeleport.Count)
            currentlyTeleportedObjects.Add(objectsToTeleport[teleportationsCount]);

        //move objects on the teleport queue
        for (int i = 0; i < currentlyTeleportedObjects.Count; i++)
        {            
            //if flag is false it should make ONLY one round
            if(!isLoop)
            {
                //Get correspondent teleporter            
                GameObject destinationTeleporter = teleporters[currentlyTeleportedObjects[i].teleportOrder];
                //set position of the item to correspondent teleporter
                currentlyTeleportedObjects[i].teleportableObject.transform.position = destinationTeleporter.transform.position;
                //so if it's not currently in the last teleporter,change it's order
                if(currentlyTeleportedObjects[i].teleportOrder < teleporters.Count - 1)
                {                        
                    currentlyTeleportedObjects[i].teleportOrder++;
                }
                //don't do nothing if not, to keep it in the same place
            }
            else //if it's true move it to random teleport
            {
                //Get random teleporter from the teleporters list
                int randomOrder = UnityEngine.Random.Range(0, teleporters.Count);
                GameObject destinationTeleporter = teleporters[randomOrder];
                //set position of the item to correspondent teleporter
                currentlyTeleportedObjects[i].teleportableObject.transform.position = destinationTeleporter.transform.position;
                //set the new order
                currentlyTeleportedObjects[i].teleportOrder = randomOrder;
            }
        }
        teleportationsCount++;
    }
}

暂无
暂无

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

相关问题 如何使用foreach循环删除面板中的所有控件? - How can I use a foreach loop to delete all of the control in a panel? 如何循环遍历层次结构中的所有游戏对象,包括两个场景中的禁用对象所有子对象? - How can I loop over all game objects in hierarchy including disabled objects all children too in both scenes? 如何使用两个progressBar控件显示每个文件的下载进度以及所有文件下载的整体进度? - How can i use two progressBar controls to display each file download progress and also overall progress of all the files download? 如何在循环中填充对象属性 - How can I fill an objects attributes in a loop 如何使用C#从列表中选择随机对象? - How can I pick random objects out of a list with C#? 如何使用 foreach 循环构建 json 对象并在 razor 中调用 jquery 函数 - How can I use a foreach loop to build json objects and call jquery functions in razor 如何在Unity 3D中使用ontrigger进行传送或门户 - How to use ontrigger for teleport or portal in Unity 3D 我可以对两个不同的声明对象使用单个 for 循环吗? - Can I use a single for loop for two different declared objects? Unity-如何使用标记确定所有对象是一次旋转还是单独旋转? - Unity - How can I use a flag to decide whether all the objects rotate at once or each one individually? 如何为我的 Unity 传送添加冷却时间 - How do I add a cooldown to my Unity teleport
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM