在我的2D游戏中,我随机化了每次运行游戏时产生4到5个克隆的对象。我的问题是我还有一个不同的对象,我想要生成1个克隆,并将它定位在我游戏中随机对象的最后一个克隆之后。
对象随机化在我的游戏中完美运行,我只需要将它与我希望它独立生成的对象分开,并在随机对象的最后一次克隆之后。
这是我使用1代尝试生成独立对象的代码:(代码取自this tutorial)
using UnityEngine;
using System;
using System.Collections.Generic; //Allows us to use Lists.
using Random = UnityEngine.Random; //Tells Random to use the Unity Engine random number generator.
namespace Completed
{
public class BoardManager : MonoBehaviour
{
// Using Serializable allows us to embed a class with sub properties in the inspector.
[Serializable]
public class Count
{
public int minimum; //Minimum value for our Count class.
public int maximum; //Maximum value for our Count class.
//Assignment constructor.
public Count (int min, int max)
{
minimum = min;
maximum = max;
}
}
public int columns = 7; //Number of columns in our game board.
public Count random1Count = new Count (1, 2); //Lower and upper limit for our random number of objects
public Count random2Count = new Count (1, 1);
public Count random3Count = new Count (1, 1);
public Count random4Count = new Count (1, 1);
public GameObject[] randomObject1; //Array of objects prefabs.
public GameObject[] randomObject2;
public GameObject[] randomObject3;
public GameObject[] randomObject4;
public GameObject obj; // the independent object declaration
private List <Vector3> gridPositions = new List <Vector3> (); //A list of possible locations to place objects.
//Clears our list gridPositions and prepares it to generate a new board.
void InitialiseList ()
{
//Clear our list gridPositions.
gridPositions.Clear ();
//Loop through x axis (columns).
for(int x = 2; x < columns; x++)
{
//At each index add a new Vector3 to our list with the x and y coordinates of that position.
gridPositions.Add (new Vector3(x, 0.3f, 0f));
Instantiate(obj); // my attempt to instantiate the separate object
Debug.Log(obj.transform.position.x); // my attempt to track the position of the separate object
}
}
//RandomPosition returns a random position from our list gridPositions.
Vector3 RandomPosition ()
{
//Declare an integer randomIndex, set it's value to a random number between 0 and the count of items in our List gridPositions.
int randomIndex = Random.Range (0, gridPositions.Count);
//Declare a variable of type Vector3 called randomPosition, set it's value to the entry at randomIndex from our List gridPositions.
Vector3 randomPosition = gridPositions[randomIndex];
//Remove the entry at randomIndex from the list so that it can't be re-used.
gridPositions.RemoveAt (randomIndex);
//Return the randomly selected Vector3 position.
return randomPosition;
}
//LayoutObjectAtRandom accepts an array of game objects to choose from along with a minimum and maximum range for the number of objects to create.
void LayoutObjectAtRandom (GameObject[] tileArray, int minimum, int maximum)
{
//Choose a random number of objects to instantiate within the minimum and maximum limits
int objectCount = Random.Range (minimum, maximum+1);
//Instantiate objects until the randomly chosen limit objectCount is reached
for(int i = 0; i < objectCount; i++)
{
//Choose a position for randomPosition by getting a random position from our list of available Vector3s stored in gridPosition
Vector3 randomPosition = RandomPosition();
//Choose a random tile from tileArray and assign it to tileChoice
GameObject tileChoice = tileArray[Random.Range (0, tileArray.Length)];
//Instantiate tileChoice at the position returned by RandomPosition with no change in rotation
Instantiate(tileChoice, randomPosition, Quaternion.identity);
}
}
//SetupScene initializes our level and calls the previous functions to lay out the game board
public void SetupScene (int level)
{
//Reset our list of gridpositions.
InitialiseList ();
//Instantiate a random number of objects based on minimum and maximum, at randomized positions.
LayoutObjectAtRandom (randomObject1, random1Count.minimum, random1Count.maximum);
LayoutObjectAtRandom (randomObject2, random2Count.minimum, random2Count.maximum);
LayoutObjectAtRandom (randomObject3, random3Count.minimum, random3Count.maximum);
LayoutObjectAtRandom (randomObject4, random4Count.minimum, random4Count.maximum);
}
}
}
答案 0 :(得分:0)
当您实例化最终的GameObject时,您并没有给它一个位置。据我所知,你想把它放在与你最后一个随机定位的GameObject相同的位置,但是在x轴上偏移。要执行此操作,您需要包含最后一个随机生成的GameObject位置的me?fields=albums{photos.limit(10){source}} // This will limit photos inside album
,我将调用此Vector3
。我假设您知道如何获得此值,如果不发表评论,我会将其包含在我的答案中。然后你可以在实例化你的最终GameObject时传递位置:
lastRandomPos
编辑:获取最后一个随机对象位置的最简单方法是每次var objPos = lastRandomPos;
objPos.x = objPos.x + <however much you want to offset in x>;
Instantiate(obj, objPos, Quaternion.identity);
Instantiate
中的对象时设置一个类级变量。在类范围内声明它,例如在LayoutObjectAtRandom
:
gridPositions
然后当你private List <Vector3> gridPositions = new List <Vector3> (); //A list of possible locations to place objects.
private Vector3 lastRandomPos;
一个随机对象时,你可以设置这个变量来跟踪它产生的位置:
Instantiate