开发Demo 一
1.角色功能
① 朝着鼠标点击方向转动:(Done)
Vector3 Move_Player(){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if(Physics.Raycast(ray, out hitInfo, 200, whatIsGround)){
Vector3 target = hitInfo.point;
target.y = transform.position.y;
transform.LookAt(target);
return target;
}
else
{
Debug.Log("error");
return Vector3.zero;
}
}
②角色射击:(Done)
void Fire()
{
Vector3 shutdir;
if (Input.GetMouseButtonDown(0))
{
bullet = (GameObject) Instantiate(Gamemanager.instance.bullet.bulletprefab,
transform.GetChild(0).position, Quaternion.identity);
shutdir = (Move_Player()-transform.position).normalized;
bullet.transform.GetComponent<Shut>().ShutDir = shutdir;
}
}
③角色对Enemy造成伤害:(Done)
private void OnTriggerEnter(Collider other)
{
DamageData data=new DamageData();
if (other.tag=="Enemy")
{
data.damage = Gamemanager.instance.bullet.Damage;
get_hit_able getHitAble=transform.GetComponent<get_hit_able>();//get_hit_able 是否可以受伤的一个脚本
if(getHitAble==null)return;
getHitAble.OnDamage(data);
}
}
get_hit_able(//可以受伤脚本):(待完善)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System;
public class DamageData
{
public float damage;
}
[Serializable]
public class Damage_Events:UnityEvent<get_hit_able,DamageData>{}
public class get_hit_able : MonoBehaviour
{
public Text Hp_text;
public Slider slider;
private float Reset_Time;
public float MaxHp;
public float Hp;
public float UnAttack_state_time;
public bool UnAttack_state=false;
private float In_UnAttack_state_time=0;
public Damage_Events On_Damage;
public Damage_Events On_Death;
public Damage_Events On_Reset;
// Start is called before the first frame update
void Start()
{
slider.maxValue=MaxHp;
Hp=MaxHp;
slider.value=Hp;
Reset_Time=2*Time.deltaTime;
}
// Update is called once per frame
void Update()
{
UnAttacked();
}
public void OnReset(){
Hp=MaxHp;
UnAttack_state=false;
In_UnAttack_state_time=0;
}
public void OnDamage(DamageData data){
if(Hp<=0||UnAttack_state)return;
Debug.LogWarning("减少"+data.damage+"滴血");
Hp-=data.damage;
Hp_text.text=Hp.ToString();
slider.value=Hp;
if(Hp<=0){
//死亡
On_Death?.Invoke(this,data);
On_Reset?.Invoke(this,null);
// Reset();
}
else{
//受伤
On_Damage?.Invoke(this,data);
}
}
public void UnAttacked(){
if(UnAttack_state){
In_UnAttack_state_time+=Time.deltaTime;
if(In_UnAttack_state_time>=UnAttack_state_time)
{
UnAttack_state=false;
In_UnAttack_state_time=0;
}
}
}
// private void Reset()
// {
// Debug.LogWarning("重置");
// if(Reset_Time<=Time.deltaTime){
// Hp=MaxHp;
// UnAttack_state=false;
// In_UnAttack_state_time=0;
// }
// }
}
④敌人AI移动:(Done)
if(Vector3.Distance(Target.position,transform.position)>=Attack_Area)
{
_navMeshAgent.SetDestination(Target.position);
transform.LookAt(Target.position);
}
else
{
if(State!="Attack")
animator.SetTrigger("Attack");
// State="Attack";
// }
}