交互
交互方式
上一次我们完成了道具类的编写,但是消耗与获得道具的交互脚本还仅限于碰撞产生光照,因此我们这次要完善一下交互代码。
鼠标移动用来控制视野方向,所以我们用鼠标左键点击来模拟交互,在鼠标点击后使用射线检测与前方物体交互。
而物体对于交互的响应虽然有各种形式,但是其可以具体分成几个大类——生成物体,销毁物体,获得道具,道具交换,使用道具。
很明显前两种之一可以和后三种之一共存,于是我们只需要留好扩展代码块,按照几个大类对其处理即可。
物体响应代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BeContacted : MonoBehaviour
{
[Range(0,3)]
public int objectType; //0为给予道具,1为交换道具,2为消耗道具,3为机关类
public int needItem;
public int[] giveItem;
public GameObject[] showGameObject;
public GameObject[] clearGameObject;
public GameObject messageShower;
public string successMessage;
public string falseMessage;
private Item item;
private void Start()
{
item = GameObject.Find("ItemManager").GetComponent<Item>();
}
public void Contact()
{
switch (objectType)
{
case 0:
foreach(int i in giveItem)
{
item.AddItem(i);
}
ObjectChange();
messageShower.