零、最终效果
飞机大战项目演示
一、需求分析
二、技术路线确定
UI面板->UGUI实现
数据存储->xml实现
核心逻辑功能->空间坐标转换、碰撞检测、资源加载等
三、关键功能实现
1、将玩家限制在屏幕内活动
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour
{
public float speed = 10f;
//移动前位置信息
private Vector3 frontPos;
//当前位置信息
private Vector3 nowPos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Move();
}
void Move()
{
frontPos = this.gameObject.transform.position;
this.gameObject.transform.Translate(Vector3.forward * Input.GetAxis("Vertical") * speed * Time.deltaTime, Space.World);
this.gameObject.transform.Translate(Vector3.right * Input.GetAxis("Horizontal") * speed * Time.deltaTime, Space.World);
//将当前位置信息转为屏幕坐标位置信息
nowPos = Camera.main.WorldToScreenPoint(this.transform.position);
//左右超出判断
if (nowPos.x<=0|| nowPos.x >=Screen.width)
{
this.transform.position = new Vector3(frontPos.x, this.transform.position.y, this.transform.position.z);
}
//上下超出判断
if (nowPos.y <= 0 || nowPos.y >= Screen.height)
{
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, frontPos.z);
}
}
}
2、子弹的不同运动方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletControl : MonoBehaviour
{
public GameObject player;
private float speed = 10f;
private float time;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//1、面朝向运动
if (Input.GetKey(KeyCode.Keypad0))
{
this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
//2、曲线运动
if (Input.GetKey(KeyCode.Keypad1))
{
//通过sin函数实现
time += Time.deltaTime;
this.gameObject.transform.Translate(Vector3.right * Mathf.Sin(time) * speed * Time.deltaTime);
this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
//3、右抛物线运动
if (Input.GetKey(KeyCode.Keypad2))
{
//改变旋转角度
this.gameObject.transform.rotation *= Quaternion.AngleAxis(speed * 10 * Time.deltaTime, Vector3.up);
this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
//4、左抛物线运动
if (Input.GetKey(KeyCode.Keypad3))
{
this.gameObject.transform.rotation *= Quaternion.AngleAxis(-speed * 10 * Time.deltaTime, Vector3.up);
this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
//5、跟踪目标运动
if (Input.GetKey(KeyCode.Keypad4))
{
//不停计算与玩家之间的方向向量 然后得到四元数,自己的角度不断朝目标四元数进行变换
this.gameObject.transform.rotation = Quaternion.Slerp(this.gameObject.transform.rotation,
Quaternion.LookRotation(player.transform.position - this.gameObject.transform.position),
speed*Time.deltaTime);
this.gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
}
}
3、鼠标射线检测销毁物体
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseDestory : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0))
{
RaycastHit hitInfo;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 1000))
{
print(hitInfo.transform.gameObject.layer);
if (hitInfo.transform.gameObject.layer==3)
{
Destroy(hitInfo.transform.gameObject);
}
}
}
}
}