【发布时间】:2020-08-05 16:29:35
【问题描述】:
开发一个小游戏,会在文本空间中生成随机名称,并且玩家应该与文本空间中显示的名称的对象发生碰撞。例如,如果显示“柠檬”,那么玩家应该触摸放在桌子上的柠檬游戏对象。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomString : MonoBehaviour
{
[SerializeField] Text randName;
public GameObject[] veggies;
string[] veg = { "Olive", "Lemon", "Carrot" };
void Start()
{
randName.text = veg[1];
}
void Update()
{
GenerateRandom(3);
}
private void GenerateRandom(int maxInt)
{
int rnd = UnityEngine.Random.Range(0, maxInt);
if (Input.GetKey(KeyCode.Space))
{
string a = veg[rnd];
randName.text = a;
}
}
void OnCollisionEnter(Collision col, string a)
{
if (col.gameObject.CompareTag(a))
{
Debug.Log("Hit");
}
}
}
【问题讨论】:
标签: c# unity3d game-development