单例模式
private GameManagers() { }//私有构造方法
private static GameManagers intance = null;//创建私有构造对象
public static GameManagers GetIntance()//公共工厂方法
{
if (intance==null)//判定对象是否为空值
{
intance = new GameManagers();//赋值
}
return intance;//返回
}
public bool haskey = false;//bool值,用来判定是否接触钥匙
public int playerlifes = 10;//玩家生命
public int playerbulletnum = 50;//玩家总共子弹数
public int playershotnum = 10;//玩家弹夹子弹数
public int enemynum = 0;//击杀敌人数量
public int score = 0;//分数
角色控制器碰撞方法
void OnControllerColliderHit(ControllerColliderHit other)//角色控制器器的碰撞方法
{
if (other.gameObject.tag.Equals(GameResources.keycardTag))//碰撞对象的标签
{
AudioSource.PlayClipAtPoint(pickUpClip, Camera.main.transform.position, 1);//碰撞音效
GameManagers.GetIntance().haskey = true;//bool值,这里表示拿到钥匙
Destroy(other.gameObject);//钥匙消失
}
if (other.gameObject.tag.Equals(GameResources.doorTag))//门的标签
{
if (GameManagers.GetIntance().haskey)//拿到钥匙
{
other.gameObject.transform.Translate(Vector3.up*1f*Time.deltaTime);//门向上打开
Invoke("DoorMove", 10);//10秒后回到原地
}
else//没有拿到钥匙
{
if (Time.time>=nestTimer)//扣血间隔
{
AudioSource.PlayClipAtPoint(alarmClip, Camera.main.transform.position, 1);//警报声
nestTimer = Time.time + timer;计算时间间隔
GameManagers.GetIntance().playerlifes -= 1;//玩家生命值减少
}
}
}
}
void DoorMove()//回到原地的方法
{
door.position = dir;
}
摄像机跟随鼠标移动
//3个枚举
// 这个表示当前控制模式,分别是
// MouseXAndY上下左右旋转
// MouseX只能左右旋转
// MouseY只能上下旋转
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
// 这俩是左右上下旋转时候的灵敏度
public float sensitivityX = 15F;
public float sensitivityY = 15F;
// 左右旋转的最大角度
public float minimumX = -360F;
public float maximumX = 360F;
//上下旋转最大角度
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update()
{
//如果是上下左右旋转的模式
if (axes == RotationAxes.MouseXAndY)
{
// 根据鼠标X轴计算摄像机 Y轴旋转角度
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
// 根据鼠标Y轴计算摄像机x轴旋转角度
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
// 检查上下旋转角度不超过 minimumY和maximumY
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
// 设置摄像机旋转角度
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)//如果只是左右旋转
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else//如果只是上下旋转
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start()
{
// 冻结刚体的旋转功能
// Make the rigid body not change rotation
if (GetComponent<Rigidbody>())
GetComponent<Rigidbody>().freezeRotation = true;
}
游戏开始时和结束的屏幕亮度调整
private Image img;
private float speed = 0.5f;
// Start is called before the first frame update
void Start()
{
img = transform.GetChild(1).transform.GetComponent<Image>();//通过判定子物体拿到对象的游戏组件
}
// Update is called once per frame
void Update()
{
img.color = Color.Lerp(img.color, Color.clear, Time.deltaTime * speed);//图片的颜色变换
}
玩家信息栏
private Image blood;
private Text score;
private Text hp;
private Text playernum;
// Start is called before the first frame update
void Start()
{
blood = transform.GetChild(5).transform.GetComponent<Image>();
score = transform.GetChild(6).transform.GetComponent<Text>();
hp = transform.GetChild(7).transform.GetComponent<Text>();
playernum=transform.GetChild(8).transform.GetComponent<Text>();通过子物体拿到对象组件
//shoot = GameObject.FindGameObjectsWithTag(GameResources.shootTag);
}
// Update is called once per frame
void Update()
{
float per=GameManagers.GetIntance().playerlifes;
blood.fillAmount = per / 10;//让图片的填充跟随玩家血量变化
hp.text = "HP:"+GameManagers.GetIntance().playerlifes.ToString();//显示玩家血量
score.text="积分:"+ GameManagers.GetIntance().score.ToString();//显示玩家积分
playernum.text = GameManagers.GetIntance().playershotnum.ToString() +"/"+ GameManagers.GetIntance().playerbulletnum.ToString(); //显示弹夹弹量和总子弹数
}