* 1. 自己做了一個小游戲在生成追擊隱藏以后重新開始游戲遇到了一些問題代碼:
//追擊玩家
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move_shootPOS : MonoBehaviour {
private int speed;
private int distance_follow = 20;//射擊目標追蹤玩家的最大距離
private GameObject player;
private Vector3 initPos;
public float bleed = 100;
public float Bleed { set; get; }
public Vector3 InitPosition
{
get { return initPos; }
set { initPos = value; }
}
void Start()
{
player = GameObject.Find("player");
// 參考玩家的正常移動速度,設定射擊目標追蹤玩家的速度
speed = player.GetComponent<move_player>().normalSpeed;
Destroy(this.gameObject, 30);
}
void OnCollisionEnter(Collision other)
{
if (other.collider.gameObject == player)
{
bleed--;
if (bleed <= 0)
{
//玩家的血量減為0以,游戲失敗
UnityEngine.SceneManagement.SceneManager.LoadScene("end");
}
}
}
// Update is called once per frame
void Update()
{
//射擊目標 實時的看向玩家
transform.LookAt(player.transform);
//判斷射擊目標距離玩家是否小于最大追蹤距離,如果是,則開始追蹤
if (Vector3.Distance(transform.position, player.transform.position) < distance_follow)
{
//設定射擊目標追蹤玩家的速度為玩家移動速度的75%
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, speed * Time.deltaTime * 0.75f);
}
}
}
```
//新生成怪物
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class target_New : MonoBehaviour {
// Use this for initialization
[SerializeField]
private GameObject m_target;
private Vector3 initPos;
void Start()
{
initPos = transform.position;
while (transform.childCount < 5)
{
CreateNewTarget();
}
}
void CreateNewTarget()
{
float x = Random.Range(-10, 10);
float z = Random.Range(-10, 10);
Vector3 pos = new Vector3(x + initPos.x, transform.position.y, z + initPos.z);
GameObject m_st = Instantiate(m_target);
m_st.transform.position = pos;
m_st.GetComponent<move_shootPOS>().InitPosition = pos;//記錄新生成射擊目標的初始位置
m_st.name = null;//string.Format("{0}_chd",this.gameObject.name);
m_st.tag = "target";
m_st.transform.parent = transform;//指定新生成的游戲物件的父物體
m_st.SetActive(false);//隱藏新生成的游戲物件
}
// Update is called once per frame
void Update()
{
if (transform.childCount < 5)
{
CreateNewTarget();
}
}
}
```
//顯示怪物
```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class target_IsShow : MonoBehaviour {
// Use this for initialization
private GameObject m_player;
void Start()
{
m_player = GameObject.Find("player");//找到玩家游戲物件
}
// Update is called once per frame
void Update()
{
//檢測玩家是否進入射擊目標的指定范圍之內,如果在范圍之內,則顯示所有射擊目標;
// 如果玩家離開了,則隱藏射擊目標
if (Vector3.Distance(transform.position, m_player.transform.position) < 20)
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(true);
}
}
else
{
for (int i = 0; i < transform.childCount; i++)
{
// transform.GetChild(i).gameObject.SetActive(false);
GameObject child = transform.GetChild(i).gameObject;
child.SetActive(false);
//隱藏射擊目標時,讓這個目標回到初始位置
child.transform.position = child.GetComponent<move_shootPOS>().InitPosition;
}
}
//當前活動的射擊目標,遠離生成點80后,自動隱藏
for (int i = 0; i < transform.childCount; i++)
{
GameObject child = transform.GetChild(i).gameObject;
if (child.activeSelf && Vector3.Distance(transform.position, child.transform.position) > 80)
{
child.SetActive(false);
//隱藏射擊目標時,讓這個目標回到初始位置
child.transform.position = child.GetComponent<move_shootPOS>().InitPosition;
}
}
}
}
```

因為運行視屏不能發出來,跪求能解決的大神幫幫忙
uj5u.com熱心網友回復:
這種問題是沒有洗掉參考uj5u.com熱心網友回復:
物件已經被銷毀了,你還在使用轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/10921.html
標籤:Unity3D
