【发布时间】:2021-12-08 23:51:04
【问题描述】:
我想根据平台(游戏对象)旋转在 2 种材质之间进行切换。 这是我到目前为止所做的:
public class PlatformSpawner : MonoBehaviour
{
public GameObject platform;
public Material[] platformMaterial;
Material currentMaterial;
Renderer _renderer;
}
void Start()
{
_renderer = this.GetComponent<Renderer>();
}
这个我也写了,但是不想按按钮换材质:
public void LeftTurn()
{
_renderer.material = platformMaterial[0];
currentMaterial = _renderer.material;
}
public void RightTurn()
{
_renderer.material = platformMaterial[1];
currentMaterial = _renderer.material;
}
}
这是平台向左或向右随机旋转 90 度的地方:
public struct SpawnPoint
{
public Vector3 position;
public Quaternion orientation;
public void Step(float distance)
{
if (Random.value < 0.5)
{
position.x += distance;
orientation = Quaternion.Euler(0, 90, 0); //change to one of the materials
}
else
{
position.z += distance;
orientation = Quaternion.Euler(0, 0, 0); //change to the other of the materials.
//This is where I want to material to switch.
//When the objects position changes, the material changes too.
}
}
}
有一张游戏画面。我想改变所有角落平台的材质以获得漂亮的曲线视图。
谁能帮助我在这种情况下该怎么做?我有点迷路了。 非常感谢您的每一次帮助!
编辑:新代码看起来像这样。唯一的问题是 Unity 给了我 15 个错误(见下图),即使 Visual Studio 说没有发现问题。错误与开关有关。
public class PlatformSpawner : MonoBehaviour
{
public GameObject platform;
public Transform lastPlatform;
SpawnPoint _spawn;
bool stop;
public Material straightMaterial;
public Material turnLeftMaterial;
public Material turnRightMaterial;
public Renderer roadPrefab;
[System.Serializable]
public struct SpawnPoint
{
public Vector3 position;
public Quaternion orientation;
public RoadType type;
public enum RoadType
{
Straight,
LeftTurn,
RightTurn
}
private enum Direction
{
Z,
X,
}
private Direction lastDirection;
public void Step(float distance)
{
type = RoadType.Straight;
if (Random.value < 0.5f)
{
position.x += distance;
orientation = Quaternion.Euler(0, 90, 0);
if (lastDirection == Direction.Z)
{
type = RoadType.RightTurn;
}
}
else
{
position.z += distance;
orientation = Quaternion.Euler(0, 0, 0);
if (lastDirection == Direction.X)
{
type = RoadType.LeftTurn;
}
lastDirection = Direction.Z;
}
}
}
void Start()
{
_spawn.position = lastPlatform.position;
_spawn.orientation = transform.rotation;
StartCoroutine(SpawnPlatforms());
}
IEnumerator SpawnPlatforms()
{
while (!stop)
{
var _spawn = new SpawnPoint();
for (var i = 0; i < 20; i++)
{
var newPlatform = Instantiate(roadPrefab, _spawn.position, _spawn.orientation);
_spawn.Step(1.5f);
var roadMaterial = _spawn.type switch
{
SpawnPoint.RoadType.LeftTurn => turnLeftMaterial,
SpawnPoint.RoadType.RightTurn => turnRightMaterial,
_ => straightMaterial
};
newPlatform.GetComponent<Renderer>().material = roadMaterial;
yield return new WaitForSeconds(0.1f);
}
}
}
}
【问题讨论】:
-
嗨,欢迎来到 SO。当前正在发生的事情,与您希望发生的事情有何不同
-
嘿,杰,谢谢你的回答!从字面上看,什么都没有发生。我玩过这个,但到目前为止没有成功。我在问题中附上了一张图片,以更好地说明我真正想要的是什么。
-
上面发布的代码不会产生所示的错误。请编辑问题以包含minimal reproducible example。
-
另外,为什么你的 while 循环中嵌套了一个 for 循环,并且还在 while 的每次迭代开始时创建一个新的生成点?当然你会想使用
var _spawn = new SpawnPoint(); while(true){ var newPlatform /* ... */ yield return new WaitForSeconds(0.1f); } -
我已经尝试了一切,它仍然是一个完整的 ***... 使用随机材料随处生成的平台。我放弃了。
标签: c# android unity3d mesh gameobject