Unity:在游戏过程中使对象随机出现或消失 [英] Unity: Make object appear or disappear randomly during game play

查看:382
本文介绍了Unity:在游戏过程中使对象随机出现或消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Unity中创建一个3D,第一人称迷宫游戏.游戏的目的是在时间到之前到达迷宫的另一侧.为了使游戏变得有趣,玩家在每回合开始迷宫贴图时都会随机生成墙,因此每个迷宫都是唯一的,因此玩家无法记住路径.我已经决定要做的是创建像这样的墙壁网格...

I'm creating a 3D, 1st person maze game in Unity. The objective of the game is to reach the other side of the maze before time is up. To make the game fun, for every round a player starts the maze map randomly generates walls so every maze is unique so the player can't memorize a path. I've decided what I should do is create a grid of walls like this...

(假设线条是墙壁,每个线段是一个单独的墙壁对象)

(Imagine that the lines are the walls, each segment is an individual wall object)

当玩家开始比赛时,每个墙段都有机会消失,所以可以想象这个网格会变成类似这样的东西……

When a player starts, each wall segment will have a chance of disappearing, so imagine that this grid will transform into something like this...

我该怎么做?我将使用C#.基本上,如果选择消失",我希望壁变为不可见并可以通过.该算法将是

How do I do that? I will be using C#. Basically, I want to wall to turn invisible and be passable if it is selected to "disappear". The algorithm will be

game starts
probability that wall will disappear = 1/3;
wall disappears = true;
if(true){
    wall becomes invisible and passable
}
 else{
    wall stays there
}

推荐答案

有很多方法可以使对象在Unity中消失.

There are many ways to make an Object disappear in Unity.

要制造的墙消失了:

public GameObject wallObject;

1 .将其停用

wallObject.SetActive(false);

2 .先禁用Wall的Renderer组件,然后禁用对撞机,以便其他GameObject可以通过它

2.Disable the Renderer component of the Wall then the collider so that other GameObjects can pass through it

wallObject.GetComponent<Renderer>().enabled = false;
wallObject.GetComponent<Collider>().enabled = false;

3 .销毁它.

Destroy(wallObject);

您可以使用 Random.Range 生成数字,然后使用以上是隐藏墙的方法之一.

You can use Random.Range to generate the number number then use one of the method above to hide that wall.

这篇关于Unity:在游戏过程中使对象随机出现或消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆