public GameObject hole;//弹孔
public Transform firePos;//开火特效位置
public GameObject fireExplosion;//开火特效
public GameObject holeExplosion;//弹孔特效
void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
GetState(Globals.PlayerState.Fire);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit, 100))
{
GameObject go=Instantiate(hole, hit.point, Quaternion.identity);//克隆弹孔
//让弹孔与射线碰撞体的法线垂直(让弹孔总是贴在物体的每一个面的表面)
go.transform.LookAt(hit.point - hit.normal);
//让弹孔与碰撞体表面保持0.01距离(避免了弹孔与碰撞体表面完全叠加从而无法完整显示)
go.transform.Translate(Vector3.back * 0.01f);
Instantiate(fireExplosion, firePos.position, Quaternion.identity);//克隆开火特效
Instantiate(holeExplosion, hit.point, Quaternion.identity);//克隆弹孔特效
如果特效不能播放,可以用粒子特效播放
ParticleSystem ps = go2.transform.GetChild(0).transform.GetComponent<ParticleSystem>();
ps.Play();
}
}
}
public GameObject hole;//弹孔
public Transform firePos;//开火特效位置
public GameObject fireExplosion;//开火特效
public GameObject holeExplosion;//弹孔特效
void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
GetState(Globals.PlayerState.Fire);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray,out hit, 100))
{
GameObject go=Instantiate(hole, hit.point, Quaternion.identity);//克隆弹孔
//让弹孔与射线碰撞体的法线垂直(让弹孔总是贴在物体的每一个面的表面)
go.transform.LookAt(hit.point - hit.normal);
//让弹孔与碰撞体表面保持0.01距离(避免了弹孔与碰撞体表面完全叠加从而无法完整显示)
go.transform.Translate(Vector3.back * 0.01f);
Instantiate(fireExplosion, firePos.position, Quaternion.identity);//克隆开火特效
Instantiate(holeExplosion, hit.point, Quaternion.identity);//克隆弹孔特效
如果特效不能播放,可以用粒子特效播放
ParticleSystem ps = go2.transform.GetChild(0).transform.GetComponent<ParticleSystem>();
ps.Play();
}
}
}
二
CharacterController cc;//角色控制器组件
Animation an;//动画组件
AudioSource audios;
AudioClip cilp,jumpClip;
AudioClip fireClip;//开枪声音
AudioClip reloadClip;//换弹夹声音
GameObject fireEffect;//开火特效
GameObject hole, explosion;//弹孔、爆炸特效
float timer = 3f;
Transform pos, nurse, dog;
GameRes.PlayerState ps = GameRes.PlayerState.Draw ;
float speed = 100;// 玩家移动速度
void Start()
{
pos = transform.GetChild(0).GetChild(0);//枪口的位置
//获取角色控制器组件
cc = GetComponent<CharacterController>();
//获取动画组件
an = GetComponent<Animation>();
audios = GetComponent<AudioSource>();
cilp = Resources.Load<AudioClip>(GameRes.walkPath);
jumpClip= Resources.Load<AudioClip>(GameRes.jumpPath);
fireEffect = Resources.Load<GameObject>(GameRes.fireEffectsPath);
hole = Resources.Load<GameObject>(GameRes.holePath);
fireClip = Resources.Load<AudioClip>(GameRes.firePath);//动态资源加载
explosion = Resources.Load<GameObject>(GameRes.poZhaEffectsPath);
reloadClip = Resources.Load<AudioClip>(GameRes.readyPath);//动态资源加载
}
void Update()
{
Move();
//设置瞄准星
if (Input.GetKeyDown(KeyCode.C))
{
DynamicGameManager.dynamic .isChange = true;
}
if (Input.GetKeyDown(KeyCode.B))
{
DynamicGameManager.dynamic .isChange = false;
}
if (Input.GetMouseButtonDown(0) && StaticGameManager.instance.currentNum != 0 && ps != GameRes.PlayerState.Reload)
{//射击
Fire();
}
if (ps == GameRes.PlayerState.Reload)
{
PlayAnimation(GameRes.PlayerState.Reload);//播放换弹夹动画
timer -= Time.deltaTime;
if (timer <= 0)
{
ps = GameRes.PlayerState.Draw ;
timer = 3f;
}
}
}
RaycastHit hit;
void Fire()
{
if (DynamicGameManager.dynamic .isChange)
{
//使用准星发射
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width * 0.5f, Screen.height * 0.5f, 0));
Physics.Raycast(ray, out hit, 100);
}
else
{
Physics.Raycast(pos.position, pos.forward, out hit, 100);
}
if (hit.collider.gameObject.CompareTag("Nurse"))
{
Destroy(hit.collider.gameObject, 0.5f);
StaticGameManager.instance.score += 10;
}
DestroyEnemy(hit.collider.gameObject);
//if (hit.collider.gameObject.CompareTag("Dog"))
//{
// Destroy(hit.collider.gameObject, 0.5f);
// StaticGameManager.instance.score++;
//}
UpdateBulletNum();
//播放开枪声音
AudioSource.PlayClipAtPoint(fireClip, pos.position);
//克隆开枪特效
GameObject go = Instantiate(fireEffect, pos.position, pos.rotation);
Destroy(go, 0.5f);
//克隆弹孔
GameObject holeGo = Instantiate(hole, hit.point, Quaternion.identity);
holeGo.transform.LookAt(hit.point - hit.normal);
holeGo.transform.Translate(Vector3.back * 0.01f);
Destroy(holeGo, 2f);
//克隆爆炸特效
GameObject explosionGo = Instantiate(explosion, hit.point, Quaternion.identity);
Destroy(explosionGo, 2f);
}
void UpdateBulletNum()
{
if (StaticGameManager .instance.currentNum >0)
{
StaticGameManager.instance.currentNum--;
if (StaticGameManager .instance .currentNum ==0&&StaticGameManager .instance .count >0)
{
StaticGameManager.instance.currentNum = 10;
ps = GameRes.PlayerState.Reload;
AudioSource.PlayClipAtPoint(reloadClip, transform.position);//播放换弹夹声音
if (StaticGameManager .instance .count >=10)
{
StaticGameManager.instance.count -= 10;
}
if (StaticGameManager.instance.count==0&& StaticGameManager.instance.currentNum ==0)
{
StaticGameManager.instance.count = 0;
StaticGameManager.instance.currentNum = 0;
}
}
}
}
void Move()
{
Vector3 dir = Vector3.zero;
if (cc.isGrounded)//判断角色是否在地面isGrounded
{
//通过键盘控制玩家的移动
float h = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
float v = Input.GetAxis("Vertical") * Time.deltaTime * speed;
//transform.forward 是物体的z轴,但是Vector3.forward是世界坐标的z轴
//dir = new Vector3(h, 0, v);
dir = transform.right * h + transform.forward * v;
if (Mathf.Abs(h) > 0 || Mathf.Abs(v) > 0)
{
PlayAnimation(GameRes.PlayerState.Draw);
脚步声
if (!audios.isPlaying)
{
audios.PlayOneShot(cilp);
}
}
if (Input.GetKeyDown(KeyCode.Space))
{
transform.position += Vector3.up * 2;
AudioSource.PlayClipAtPoint(jumpClip, transform.position);
}
}
cc.SimpleMove(dir);
}
void PlayAnimation(GameRes.PlayerState ps)
{
switch (ps)
{
case GameRes.PlayerState.Draw:
an.Play("Draw");
break;
case GameRes.PlayerState.Fire:
an.Play("Fire");
break;
case GameRes.PlayerState.Reload:
an.Play("Reload");
break;
case GameRes.PlayerState.Melee:
an.Play("Melee");
break;
default:
break;
}
}