开发软件: unity3d 、 vs2017
游戏简介: 在3D模式下开发的2D游戏,使用UGUI进行场景搭建,适合有一定unity基础的同学学习制作
●首先放上完成的图
●然后再放上主要功能的代码
控制枪的行为
using UnityEngine;
public class GunFollow : MonoBehaviour
{
public RectTransform UGUICanvas;
public Camera mainCamera;
void Update()
{
Vector3 mousePos;
RectTransformUtility.ScreenPointToWorldPointInRectangle(UGUICanvas, new Vector2
(Input.mousePosition.x, Input.mousePosition.y), mainCamera, out mousePos);
float z;
if (mousePos.x > transform.position.x)
{
z = -Vector3.Angle(Vector3.up, mousePos - transform.position);
}
else
{
z = Vector3.Angle(Vector3.up, mousePos - transform.position);
}
transform.localRotation = Quaternion.Euler(0, 0, z);
}
}
生成鱼及控制鱼的行为
using UnityEngine;
using System.Collections;
public class FishMaker : MonoBehaviour
{
public Transform fishHolder;
public Transform[] genPositions;
public GameObject[] fishPrefabs;
public float fishGenWaitTime = 0.5f;
public float waveGenWaitTime = 0.3f;
void Start()
{
InvokeRepeating("MakeFishes", 0, waveGenWaitTime);
}
void MakeFishes()
{
int genPosIndex = Random.Range(0, genPositions.Length);
int fishPreIndex = Random.Range(0, fishPrefabs.Length);
int maxNum = fishPrefabs[fishPreIndex].GetComponent<FishAttr>().maxNum;
int maxSpeed = fishPrefabs[fishPreIndex].GetComponent<FishAttr>().maxSpeed;
int num = Random.Range((maxNum / 2) + 1, maxNum);
int speed = Random.Range(maxSpeed / 2, maxSpeed);
int moveType = Random.Range(0, 2); //0:直走;1:转弯
int angOffset; //仅直走生效,直走的倾斜角
int angSpeed; //仅转弯生效,转弯的角速度
if (moveType == 0)
{
angOffset = Random.Range(-22, 22);
StartCoroutine(GenStraightFish(genPosIndex, fishPreIndex, num, speed, angOffset));
}
else
{
if (Random.Range(0, 2) == 0) //是否取负的角速度
{
angSpeed = Random.Range(-15, -9);
}
else
{
angSpeed = Random.Range(9, 15);
}
StartCoroutine(GenTrunFish(genPosIndex, fishPreIndex, num, speed, angSpeed));
}
}
IEnumerator GenStraightFish(int genPosIndex,int fishPreIndex,int num,int speed,int angOffset)
{
for (int i = 0; i < num; i++)
{
GameObject fish = Instantiate(fishPrefabs[fishPreIndex]);
fish.transform.SetParent(fishHolder, false);
fish.transform.localPos