есть ENEMY, с AI
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class EnemyAI: MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotSpeed;
public float minDis;
private Transform myTransform;
void Awake(){
myTransform = transform;
}
void Start () {
target = null;
}
void Update () {
if(target){
float dist = Vector3.Distance(target.position,myTransform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation
(target.position-myTransform.position),
rotSpeed*Time.deltaTime);
if(dist>minDis){
myTransform.Translate(0,0,moveSpeed*Time.deltaTime);
}
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Player")){
target = other.transform;
}
}
void OnTriggerExit(Collider other){
if(other.CompareTag("Player")){
target = null;
}
}
}
using System.Collections;
public class EnemyAI: MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotSpeed;
public float minDis;
private Transform myTransform;
void Awake(){
myTransform = transform;
}
void Start () {
target = null;
}
void Update () {
if(target){
float dist = Vector3.Distance(target.position,myTransform.position);
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation
(target.position-myTransform.position),
rotSpeed*Time.deltaTime);
if(dist>minDis){
myTransform.Translate(0,0,moveSpeed*Time.deltaTime);
}
}
}
void OnTriggerEnter(Collider other){
if(other.CompareTag("Player")){
target = other.transform;
}
}
void OnTriggerExit(Collider other){
if(other.CompareTag("Player")){
target = null;
}
}
}
и статус барами
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class NPSStatusBar : MonoBehaviour {
public int maxH;
public int maxM;
public int maxE;
private int _curH;
private int _curM;
private int _curE;
private float _StatBarL;
private bool _showBars;
// Use this for initialization
void Start () {
//health
if(maxH<1)maxH=1;
_curH = maxH;
//mana
if(maxM<1)maxM=1;
_curM = maxM;
//edurance
if(maxE<1)maxE=1;
_curE = maxE;
//////////////////
_StatBarL = Screen.width/6;
_showBars = false;
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
if(_curH<=0)
{
HideBars();
}
else
{
if(_showBars){
GUI.Box(new Rect(Screen.width/2-_StatBarL/2,10,_StatBarL,20),"health"+_curH+"/"+maxH);
GUI.Box(new Rect(Screen.width/2-_StatBarL/2,40,_StatBarL,20),"mana"+_curM+"/"+maxM);
GUI.Box(new Rect(Screen.width/2-_StatBarL/2,70,_StatBarL,20),"edurance"+_curE+"/"+maxE);
}
}
}
public void Change_curH(int chH){
_curH -= chH;
if(_curH<0)_curH = 0;
if(_curH>maxH)_curH = maxH;
}
public void Change_curM(int chM){
_curM -= chM;
if(_curM<0)_curM = 0;
if(_curM>maxM)_curM = maxM;
}
public void Change_curE(int chE){
_curE -= chE;
if(_curE<0)_curE = 0;
if(_curE>maxE)_curE = maxE;
}
public void ShowBars(){
_showBars = true;
}
public void HideBars(){
_showBars = false;
}
}
using System.Collections;
public class NPSStatusBar : MonoBehaviour {
public int maxH;
public int maxM;
public int maxE;
private int _curH;
private int _curM;
private int _curE;
private float _StatBarL;
private bool _showBars;
// Use this for initialization
void Start () {
//health
if(maxH<1)maxH=1;
_curH = maxH;
//mana
if(maxM<1)maxM=1;
_curM = maxM;
//edurance
if(maxE<1)maxE=1;
_curE = maxE;
//////////////////
_StatBarL = Screen.width/6;
_showBars = false;
}
// Update is called once per frame
void Update () {
}
void OnGUI(){
if(_curH<=0)
{
HideBars();
}
else
{
if(_showBars){
GUI.Box(new Rect(Screen.width/2-_StatBarL/2,10,_StatBarL,20),"health"+_curH+"/"+maxH);
GUI.Box(new Rect(Screen.width/2-_StatBarL/2,40,_StatBarL,20),"mana"+_curM+"/"+maxM);
GUI.Box(new Rect(Screen.width/2-_StatBarL/2,70,_StatBarL,20),"edurance"+_curE+"/"+maxE);
}
}
}
public void Change_curH(int chH){
_curH -= chH;
if(_curH<0)_curH = 0;
if(_curH>maxH)_curH = maxH;
}
public void Change_curM(int chM){
_curM -= chM;
if(_curM<0)_curM = 0;
if(_curM>maxM)_curM = maxM;
}
public void Change_curE(int chE){
_curE -= chE;
if(_curE<0)_curE = 0;
if(_curE>maxE)_curE = maxE;
}
public void ShowBars(){
_showBars = true;
}
public void HideBars(){
_showBars = false;
}
}
и FPS контроллер с "targeting"
Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class TargetingPlayer : MonoBehaviour {
//public Transform myTransform;
public Transform targetE;
void Start(){
//myTransform = transform;
targetE = null;
}
void OnTriggerEnter(Collider other1){
if(other1.CompareTag("Enemy")){
targetE = other1.transform;
NPSStatusBar NPSSB = (NPSStatusBar)targetE.gameObject.GetComponent("NPSStatusBar");
NPSSB.ShowBars();
}
}
void OnTriggerExit(Collider other1){
if(other1.CompareTag("Enemy")){
NPSStatusBar NPSSB = (NPSStatusBar)targetE.gameObject.GetComponent("NPSStatusBar");
NPSSB.HideBars();
targetE = null;
}
}
}
using System.Collections;
public class TargetingPlayer : MonoBehaviour {
//public Transform myTransform;
public Transform targetE;
void Start(){
//myTransform = transform;
targetE = null;
}
void OnTriggerEnter(Collider other1){
if(other1.CompareTag("Enemy")){
targetE = other1.transform;
NPSStatusBar NPSSB = (NPSStatusBar)targetE.gameObject.GetComponent("NPSStatusBar");
NPSSB.ShowBars();
}
}
void OnTriggerExit(Collider other1){
if(other1.CompareTag("Enemy")){
NPSStatusBar NPSSB = (NPSStatusBar)targetE.gameObject.GetComponent("NPSStatusBar");
NPSSB.HideBars();
targetE = null;
}
}
}
У enemy есть spere collider (is Trigger), когда игрок входит в тригер енеми поворачивается и преследует его до тех пор пока игрок не покинет тригер.(отлично работает)
У игрока в свою очередь есть капсул колайдер(тоже тригер), "смотрящий вперед". И вот, когда в этот тригер попадает енеми должны отобразиться статус бары енеми. Смоти не смотри на енеми ничего не отображается, а вот если я вхожу в тригер енеми, то все статус бары отображаются, выхожу - скрываются, а должно только при взгляде. Тригер "взгляда" цеплял и на FPS player и на камеру, ни каких результатов. Помогите разобраться, что не так я делаю. P.S. читал темку про взгляд с помощью view frustum, но мне надо разобраться именно с тригерами.