PS:本系列笔记将会记录我此次在北京学习Unity开发的总体过程,方便后期写总结,笔记为日更。
笔记内容均为 自己理解,不保证每个都对。
Demo地址:
Part 1 Transform组件:
常用属性:
属性名 | 作用 |
position | 世界坐标系中的坐标 |
localPostion | 相对于父类的坐标 |
eulerAngles | 相对于世界的欧拉角 |
localEulerAngles | 相对于父类的欧拉角 |
parent | 返回父类 |
例如:
Vector3 temp = new Vector3(1, 1, 1);
AI.transform.position = tempVector; //设置生成AI的坐标
bullet.transform.SetParent(transform, false); //设置父类为当前游戏物体
Part 2 Input:
输入主要包括:键盘 ,鼠标 , 手指 , 摇杆,手柄。
键盘输入: 括号内是 KeyCode.按键
属性 | 操作 |
Input.GetKeyDown() | 按键按下,触发一次 |
Input.GetKeyUp() | 按键松开,触发一次 |
Input.GetKey() | 按键按下, 一直触发 |
鼠标输入: 括号内是 0鼠标左键, 1鼠标右键, 2鼠标中键
属性 | 操作 |
Input.GetMouseButtonDown() | 按键按下,触发一次 |
Input.GetMouseButtonUp() | 按键松开,触发一次 |
Input.GetMouseButton() | 按键按下, 一直触发 |
轴输入: Mouse X (水平轴) Mouse Y (垂直轴) Mouse ScrollWheel (鼠标中键滚动)
属性 | 操作 |
Input.GetAxis | |
Input.GetAxisRaw) |
自定义设置属性:Edit->Project Setting->input
Part 3 获得物体:
1,GameObject.Find(“Player”) 通过名字
GameObject target = GameObject.Find("TankPlayer"); //寻找玩家
2,GameObject.FindGameObjectWithTag(“Player”) 通过标签
GameObject TankAiS = GameObject.FindGameObjectWithTag("AI"); //用GameObejct数组找到场景内所有 Tag 为AI的物体
3,GameObject.FindGameObjectsWithTag(“Player”) 通过标签 找多个物体
GameObject[] TankAiS = GameObject.FindGameObjectsWithTag("AI"); //用GameObejct数组找到场景内所有 Tag 为AI的物体
4,Public
脚本内声明,脚本外拖入
实例化物体过程:
1,物体放入 Resources 中
2,Object tmpObj= Resources.Load(“预制体名”); 加载到内存中
3,GameObject tmpGameObj= GameObject.Instantiate(tmpObj) as GameObject 实例化到
场景中。
Part 4 坦克大战DEMO:
AIsetting:时刻触发
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AiSetting : MonoBehaviour
{
// Start is called before the first frame update
float timeCount = 0f;
Transform target;
int cnt = 0;
public int GetCnt //获得当前AI数量
{
get
{
return cnt;
}
set
{
cnt = value;
}
}
void Start()
{
target = GameObject.Find("TankPlayer").transform; //获得玩家的 transform信息
}
// Update is called once per frame
void Update()
{
timeCount += 0.1f;
//Debug.Log(timeCount);
if(timeCount > 20f && cnt <= 10) //每隔20f并且场景AI数量 <= 10 时 实例化一个AI
{
Object TankAi = Resources.Load("TankAI"); //实例化到内存
GameObject AI = GameObject.Instantiate(TankAi) as GameObject; //实例化到场景
Vector3 tempVector = target.transform.position;
tempVector.x += Random.Range(5, 10); //随机生成X轴
tempVector.z += Random.Range(4, 20); //随机生成Y轴
AI.transform.position = tempVector; //设置生成AI的坐标
timeCount = 0; //计时器归零
cnt++;
//Debug.LogWarning(cnt);
}
}
}
BulletMovement子弹运动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletMovement : MonoBehaviour
{
// Start is called before the first frame update
float timeCount = 0f;
void Start()
{
this.transform.parent = null; //父类为空
}
// Update is called once per frame
void Update()
{
this.transform.Translate(new Vector3(0, 0.5f, 0), Space.Self);
timeCount += 0.1f;
if(timeCount >= 5f)
{
GameObject.Destroy(gameObject);
timeCount = 0;
}
}
}
Shoot 子弹射击:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shoot : MonoBehaviour
{
// Start is called before the first frame update
public float speed = 0.5f;
float timeCount = 0f;
void Start()
{
}
// Update is called once per frame
public bool IsAI; //判断是否为AI
GameObject bullet; //子弹
public void PlayerAttack() //玩家攻击
{
if (Input.GetMouseButtonDown(0)) //鼠标左键按下
{
Attack();
}
}
public void Attack() //实例化子弹
{
Object tmpBullet = Resources.Load("Bullet"); //实例化到内存
bullet = GameObject.Instantiate(tmpBullet) as GameObject; //实例化到场景
bullet.transform.SetParent(transform, false);
}
public void Check()
{
if(IsAI == true)
{
GameObject target = GameObject.Find("TankPlayer"); //寻找玩家
float tmpDis = Vector3.Distance(bullet.transform.position, target.transform.position); //计算子弹和玩家的距离
if(tmpDis < 1f) //模拟碰撞
{
GameObject.Destroy(bullet);
target.GetComponent<TankMovement>().GetHP -= 1; //玩家HP - 1
//Debug.Log(target.GetComponent<TankMovement>().GetHP);
}
}
else
{
GameObject[] TankAiS = GameObject.FindGameObjectsWithTag("AI"); //用GameObejct数组找到场景内所有 Tag 为AI的物体
for (int i = 0; i < TankAiS.Length; i++)
{
float tmpDis = Vector3.Distance(bullet.transform.position, TankAiS[i].transform.position); //子弹距离 第I个AI的距离
if (tmpDis < 2f) //模拟碰撞
{
GameObject.Destroy(bullet);
TankAiS[i].GetComponent<AIMovement>().GetHP -= 20; //AI血量 - 20
Debug.Log("AI HP == " + TankAiS[i].GetComponent<AIMovement>().GetHP);
}
}
}
}
void Update()
{
if (bullet != null)
{
Check();
}
if (IsAI == false)
PlayerAttack();
else
{
timeCount += 0.1f;
//Debug.LogWarning(timeCount);
if(timeCount > 10f) //AI每隔10f更新一次
{
timeCount = 0;
Attack();
}
}
}
}
AIMovement AI移动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AIMovement : MonoBehaviour
{
// Start is called before the first frame update
GameObject target;
public float HP = 100; //设置血量
public float GetHP
{
get
{
return HP;
}
set
{
HP = value;
//Debug.LogWarning(HP);
}
}
public void Dead()
{
AiSetting tmpSet = GetComponent<AiSetting>(); //获得AiSetting属性
if (tmpSet != null)
tmpSet.GetCnt--; //AI数目- 1
if (HP <= 0)
GameObject.Destroy(gameObject); //销毁当前AI
}
void Start()
{
target = GameObject.Find("TankPlayer");
}
// Update is called once per frame
void Update()
{
Dead();
this.transform.LookAt(target.transform);
}
}
TankMovement 坦克移动:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankMovement : MonoBehaviour
{
// Start is called before the first frame update
public float HP = 100;
public float GetHP
{
get
{
return HP;
}
set
{
HP = value;
}
}
public void Dead()
{
if(HP <= 0)
GameObject.Destroy(gameObject);
}
void Start()
{
}
// Update is called once per frame
void Update()
{
Dead();
if (Input.GetKey(KeyCode.W)) //W键按下
{
//Debug.Log("W comming!!!");
this.transform.Translate(new Vector3(0, 0, 1F) * Time.deltaTime , Space.Self);
}
if (Input.GetKey(KeyCode.S)) //S键按下
{
//Debug.Log("S comming!!!");
this.transform.Translate(new Vector3(0, 0, -1F) * Time.deltaTime, Space.Self);
}
if (Input.GetKey(KeyCode.A)) //A键按下
{
//Debug.Log("A comming!!!");
this.transform.Rotate(new Vector3(0, -0.5f, 0), Space.Self);
}
if (Input.GetKey(KeyCode.D)) //D键按下
{
//Debug.Log("D comming!!!");
this.transform.Rotate(new Vector3(0, 0.5f, 0), Space.Self);
}
}
}