using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
CharacterController cc;
Animation an;
AudioSource audios;
AudioClip clip,fireclip;
Transform pos, dog;
GameObject hole, f_effect, b_effect;
void Start () {
dog = GameObject.FindWithTag(GameRes .dog ).transform ;
pos = transform.GetChild(0).GetChild(0);
cc = GetComponent<CharacterController >();
audios = GetComponent<AudioSource>();
an = GetComponent<Animation >();
clip = Resources.Load<AudioClip >(GameRes .jiaobPath );
fireclip = Resources.Load<AudioClip >(GameRes .player_firePath );
hole = Resources.Load<GameObject>(GameRes.holePath );
f_effect = Resources.Load<GameObject>(GameRes.fire_effectPath );
b_effect = Resources.Load<GameObject>(GameRes.baoza_effectPath );
}
void Update () {
Move();
Fire();
if (dog)
{
Dir();
}
}
void PlayAnimation(PlayerAnimation pa )
{
switch (pa)
{
case PlayerAnimation.Idle:
an.Play("Idle");
break;
case PlayerAnimation.Fire:
an.Play("Fire");
break;
case PlayerAnimation.Melee:
an.Play("Melee");
break;
case PlayerAnimation.Reload:
an.Play("Reload");
break;
default:
break;
}
}
void Move()
{
float h = Input.GetAxis("Horizontal") * Time.deltaTime * 300;
float v = Input.GetAxis("Vertical") * Time.deltaTime * 300;
Vector3 dir = transform.right * h + transform.forward * v;//保证正常移动
if (cc.isGrounded)
{
if (Mathf.Abs(h) > 0 || Mathf.Abs(v) > 0)
{
PlayAnimation(PlayerAnimation.Idle);
if (!audios .isPlaying )
{
audios.PlayOneShot(clip);
}
}
}
cc.SimpleMove(dir);
}
void Fire()
{
Debug.DrawRay(pos.position, pos.forward);//用于测试射线的位置及方向
if (Input.GetMouseButtonDown (0))
{
PlayAnimation(PlayerAnimation.Fire);
AudioSource.PlayClipAtPoint(fireclip, pos.position);
RaycastHit hit;
if( Physics.Raycast(pos.position, pos.forward, out hit, 100))
{
//克隆开火特效
GameObject fgo = Instantiate(f_effect, pos.position, transform .rotation );
Destroy(fgo, 0.25f);
//克隆弹孔
GameObject hgo = Instantiate(hole, hit.point, Quaternion .identity );
hgo.transform.LookAt(hit.point - hit.normal );//改变弹孔的方向,达到能贴在射击点效果
hgo.transform.Translate (Vector3 .back *0.01f);//将弹孔往后移一点,优化射击效果
Destroy(hgo, 1.5f);
//克隆爆炸特效
GameObject bgo = Instantiate(b_effect, hit.point,hit.transform .rotation );
Destroy(bgo, 1.5f);
//射线碰撞到dog,使其hp减少
if (hit.collider.CompareTag(GameRes.dog))
{
hit.collider.gameObject.GetComponent<Dog>().hp--;
}
}
}
}
void Dir()
{
float a = Vector3.Distance(transform.position, dog .position);
float b = Vector3.Dot(transform.forward , (dog.position-transform .position).normalized );
Vector3 c = Vector3.Cross(transform.position, dog.position);
if (a<=10)
{
gameManger.gm.f = true;
if (b>0)
{
if (c.y>0)
{
gameManger.gm.s= "敌人出现在右前方";
}
else if(c.y<0)
{
gameManger.gm.s = "敌人出现在左前方";
}
}
else if (b<0)
{
if (c.y > 0)
{
gameManger.gm.s = "敌人出现在右后方";
}
else if (c.y < 0)
{
gameManger.gm.s = "敌人出现在左后方";
}
}
}
if (a <= 3)
{
gameManger.gm.f1 = true;
}
}
}
|