[英]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.