这里检测不到碰撞的原因是之前在ProjectSetting设置Physics时禁用了Enemy与除Turret层之外的碰撞,只需将Bullet也设为Turret层即可
这里检测不到碰撞的原因是之前在ProjectSetting设置Physics时禁用了Enemy与除Turret层之外的碰撞,只需将Bullet也设为Turret层即可
不是设置碰撞检测矩阵了么,自己忘了?
老师,这里检测不出来的原因应该是你刚才设置的physical里面的检测layer。
你没有设置 子弹和怪物的layer可以检测。
= =、是不是???
在为Toggle选择 OnValueChange 事件选择处理函数时,一定要选择 “Dynamic bool” 部分的函数。
如果选择下面部分的内容,处理函数会一直得到的isON值为True
鼠标交互,物体上面需要有Collider,材质需要变颜色得话,需要使用自己创建得材质。
鼠标移到物体上
OnMouseEnter()
OnMouseExit()
检测鼠标是否在UI上
using UnityEngine;
using UnityEngine.EventSystems;
EventSystem.current.IsPointOverGameObject()
把物体的材质设为红色
private Renderer rendererer ;
renderer = GetComent<Renderer>();
renderer.material.color = color.red;
using UnityEngine;
public class ViewController : MonoBehaviour
{
public float speed = 25.0f;
public float mouseSpeed = 600.0f;
void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
float mouse = Input.GetAxis("Mouse ScrollWheel");
transform.Translate(new Vector3(h * speed, mouse * mouseSpeed, v * speed) * Time.deltaTime, Space.World);
}
}
project中创建动画animate,money -》window-》adnimation
弹出框中create,然后保存在money文件夹
构建动画,闪烁效果120帧一次变换,然后在moeny中添加animator在流程图中将动画流程重新梳理,创建一个空始发器,然后指向flick闪烁动画,flck取消loop反复调用,指向空始发器
创建triggle中的flicker
在buildmanager脚本中创建一个animator对象,在compoent中将money与该对象关联
moneyanimater.settrigger("flicker")
unity粒子效果
创建buildereffect 创建paticlesysytem
standardturret 加入到工程
添加脚本
添加一个球体碰撞器 sphere collider
y:0.5,radius调整大小范围
添加ragigle刚体
在脚本中创建进出球体方法
通过标签判断进来的是不是球体
3个enemy添加tag标签,并附着标签
pubulic list<Gameobject>enemys = new
if(col.tag == "enemy")
性能优化 在turret添加layer 的enemy,turret层
edit-prohectsetting-phsic
炮台上加子弹发射器
turret脚本中创建 炮弹发射频率
定义定时器,达到时间间隔,并且满足enemys.cout敌人数量大于0
在ui上添加一个空的gameobject作为子弹命名为fireposition
在turret创建turrent的头部添加子弹,子弹定义为一个新的gameobject,命名为bulldprefab
在turret创建turrent的transform,用于定义fireposition获取炮弹的位置,定义为pubulic以托捏关联
因为后期的炮弹跟随炮台上半部分移动,所以这里的transform需要重新第一个新的与炮台上体同步
standardcurret 要应用下apply
添加子弹bullet,渲染颜色大小,放入到prefab后再进行,托捏赋值
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 10.0f;
private Transform[] positions;
private int index = 0;
void Start()
{
positions = Waypoints.positions;
}
void Update()
{
Move();
}
private void Move()
{
if (index > positions.Length - 1) return;
transform.Translate((positions[index].position - transform.position).normalized * speed * Time.deltaTime);
if (Vector3.Distance(positions[index].position, transform.position) < 0.2f)
{
index++;
}
}
}
using UnityEngine;
//保存每一波敌人生成所需要的属性
[System.Serializable]
public class Wave
{
public GameObject enemyPrefab;
public int count;
public float rate;
}
using System.Collections;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public Wave[] waves;
public Transform START;
public float waveRate = 3.0f;
private void Start()
{
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy()
{
foreach (Wave wave in waves)
{
for (int i = 0; i < wave.count; i++)
{
GameObject.Instantiate(wave.enemyPrefab, START.position, Quaternion.identity);
if (i != wave.count - 1)
yield return new WaitForSeconds(wave.rate);
}
yield return new WaitForSeconds(waveRate);
}
}
}
序列化的类才能显示在检视面板上。
using System.Collections;
using UnityEngine;
public class EnemySpawner : MonoBehaviour
{
public static int CountEnemyAlive = 0;
public Wave[] waves;
public Transform START;
public float waveRate = 0.2f;
private void Start()
{
StartCoroutine(SpawnEnemy());
}
IEnumerator SpawnEnemy()
{
foreach (Wave wave in waves)
{
for (int i = 0; i < wave.count; i++)
{
GameObject.Instantiate(wave.enemyPrefab, START.position, Quaternion.identity);
CountEnemyAlive++;
if (i != wave.count - 1)
yield return new WaitForSeconds(wave.rate);
}
while(CountEnemyAlive>0)
{
yield return 0;
}
yield return new WaitForSeconds(waveRate);
}
}
}
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed = 10.0f;
private Transform[] positions;
private int index = 0;
void Start()
{
positions = Waypoints.positions;
}
void Update()
{
Move();
}
private void Move()
{
if (index > positions.Length - 1) return;
transform.Translate((positions[index].position - transform.position).normalized * speed * Time.deltaTime);
if (Vector3.Distance(positions[index].position, transform.position) < 0.2f)
{
index++;
}
if(index > positions.Length - 1)
{
ReachDestination();
}
}
private void ReachDestination()
{
GameObject.Destroy(this.gameObject);
}
private void OnDestroy()
{
EnemySpawner.CountEnemyAlive--;
}
}
Toggle Group ---单选按钮分组
为什么不能移动,你看看你的输入法,现在是中文状态,怎么移动?
改成英文输入法的状态就可以移动。
望向攻击目标要控制Z轴
using UnityEngine;
public class TurretData
{
public GameObject turretPrefab;
public int cost;
public GameObject turretUpgradePrefab;
public int costUpgraded;
public TurretType type;
}
public enum TurretType
{
LaserTurret,
MissileTurret,
StandardTurret
}
public static Transform[] position;
positions = new Transform[transform.childCount];
for (int i = 0; i < positionLength; i++){
}
数据类需要序列化[System.Serializable]
using UnityEngine;
[System.Serializable]
public class TurretData
{
public GameObject turretPrefab;
public int cost;
public GameObject turretUpgradePrefab;
public int costUpgraded;
public TurretType type;
}
public enum TurretType
{
LaserTurret,
MissileTurret,
StandardTurret
}
using UnityEngine;
public class BuildManager : MonoBehaviour
{
public TurretData laserTurretData;
public TurretData missileTurretData;
public TurretData standardTurretData;
//表示当前选择的炮台(要建造的炮台)
private TurretData selectedTurretData;
public void OnLaserSelected(bool isOn)
{
if(isOn)
{
selectedTurretData = laserTurretData;
}
}
public void OnMissileSelected(bool isOn)
{
if(isOn)
{
selectedTurretData = missileTurretData;
}
}
public void OnStandardSelected(bool isOn)
{
if(isOn)
{
selectedTurretData = standardTurretData;
}
}
}