今天我有一个困扰我几个小时的问题。我在youtube上看了一个关于磁场的教程,我发现了它并且我遇到了问题。在那个实现中,我必须制作一个将被吸引的对象和另一个将吸引的对象。被吸引的那个我必须创建一个空的游戏对象并给它一个碰撞器,其中写入是触发器。现在的问题是我有一个健康脚本,所以当玩家用他的身体接触任何物体时他会失去健康。这个球体对撞机是玩家的一部分,每当它接触到身体时,玩家就会失去健康。我需要帮助,因为球体对撞机有一个非常大的半径,每当它碰到游戏对象时,玩家就会失去健康。我需要球体对撞机,当它触及游戏对象时,不要让玩家失去健康,请帮忙!
void Start ()
{
cur_Health = max_Health;
cur_Fuel = max_Fuel;
Time.timeScale = 0f;
StartP.SetActive (true);
score = 0;
UpdateScore ();
FinishP.SetActive (false);
LoseP.SetActive (false);
/*StartCoroutine (SpawnWaves ());
if(PlayerPrefs.GetInt("HighScore") != null)
{
highscore = PlayerPrefs.GetInt ("HighScore");
}*/
}
public void PlayButton()
{
Start ();
Time.timeScale = 1f;
StartP.SetActive (false);
}
public void NextButton()
{
Application.LoadLevel (1);
}
void Update()
{
if (Input.GetKeyDown (KeyCode.R))
{
Application.LoadLevel (0);
}
if (Input.GetKeyDown (KeyCode.Q))
{
Application.LoadLevel (1);
}
cur_Fuel -= 0.04f;
float calc_Fuel = cur_Fuel / max_Fuel;
SetFuelBar (calc_Fuel);
if(score >= checkpoint && cur_Health >= H && cur_Fuel >= F)
{
FinishP.SetActive (true);
}
else if(score >= checkpoint && cur_Health <= H && cur_Fuel <= F )
{
LoseP.SetActive (true);
Time.timeScale = 0f;
}
if(cur_Fuel <= 0f)
{
death += 1;
if (death == 1)
{
heart.enabled = false;
FuelBar.transform.localScale = new Vector3 (1,1,1);
cur_Fuel = 100f;
}
else if (death == 2)
{
heart1.enabled = false;
FuelBar.transform.localScale = new Vector3 (1,1,1);
cur_Fuel = 100f;
}
else if(death == 3)
{
FuelBar.transform.localScale = new Vector3 (0,1,1);
camera.transform.parent = null;
Instantiate (playerExplosion,transform.position, transform.rotation);
heart2.enabled = false;
LoseP.SetActive (true);
}
}
if(cur_Fuel >= 100f)
{
FuelBar.transform.localScale = new Vector3 (1,1,1);
cur_Fuel = 100f;
}
}
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Magnet")
{
cur_Health -= 1f;
float calc_Health = cur_Health / max_Health;
SetBar (calc_Health);
}
if(col.gameObject.tag == "Barrir")
{
cur_Health -= 60f;
float calc_Health = cur_Health / max_Health;
SetBar (calc_Health);
}
if(col.gameObject.tag == "Fuel")
{
cur_Fuel += 20f;
float calc_Fuel = cur_Fuel / max_Fuel;
SetFuelBar (calc_Fuel);
Destroy (col.gameObject);
}
if(col.gameObject.tag == "Enemy" || col.gameObject.tag == "Asteroid2" || col.gameObject.tag == "Asteroid3")
{
cur_Health -= 30f;
float calc_Health = cur_Health / max_Health;
SetBar (calc_Health);
}
if(col.gameObject.tag == "Black")
{
cur_Health -= 45f;
float calc_Health = cur_Health / max_Health;
SetBar (calc_Health);
}
if (col.gameObject.tag == "Black1")
{
if(heart.enabled == true && heart1.enabled == true && heart2.enabled == true)
{
heart.enabled = false;
Bar.transform.localScale = new Vector3 (1,1,1);
cur_Health = 100f;
}
else if(heart1.enabled == true && heart2.enabled == true)
{
heart1.enabled = false;
Bar.transform.localScale = new Vector3 (1,1,1);
cur_Health = 100f;
}
else if(heart2.enabled == true)
{
heart2.enabled = false;
Bar.transform.localScale = new Vector3 (0,1,1);
camera.transform.parent = null;
Instantiate (playerExplosion,col.transform.position, col.transform.rotation);
LoseP.SetActive (true);
}
}
if(col.gameObject.tag == "Health")
{
cur_Health += 15f;
float calc_Health = cur_Health / max_Health;
SetBar (calc_Health);
Destroy (col.gameObject);
}
if(cur_Health <= 0f)
{
death += 1;
if (death == 1)
{
heart.enabled = false;
Bar.transform.localScale = new Vector3 (1,1,1);
cur_Health = 100f;
}
else if (death == 2)
{
heart1.enabled = false;
Bar.transform.localScale = new Vector3 (1,1,1);
cur_Health = 100f;
}
else if(death == 3)
{
Bar.transform.localScale = new Vector3 (0,1,1);
camera.transform.parent = null;
Instantiate (playerExplosion,col.transform.position, col.transform.rotation);
heart2.enabled = false;
LoseP.SetActive (true);
}
}
if(cur_Health >= 100f)
{
Bar.transform.localScale = new Vector3 (1,1,1);
cur_Health = 100f;
}
}
void SetBar(float myhealth)
{
Bar.transform.localScale = new Vector3 (myhealth,Bar.transform.localScale.y,Bar.transform.localScale.z);
}
void SetFuelBar(float myFuel)
{
FuelBar.transform.localScale = new Vector3 (myFuel,FuelBar.transform.localScale.y,FuelBar.transform.localScale.z);
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
答案 0 :(得分:2)
所以这看起来似乎搞乱了。首先,我会建议你阅读here和here的答案,以了解你游戏中发生的事情。
现在让我们根据您在问题中分享的信息尝试解决问题。我假设磁体对象(由于大球体而被吸引)具有标记“磁铁”,因此当您的球对撞机与其碰撞时,OnTriggerEnter()
方法中的以下代码会降低健康状况:
if(col.gameObject.tag == "Magnet")
{
cur_Health -= 1f;
float calc_Health = cur_Health / max_Health;
SetBar (calc_Health);
}
如果您在拾取磁铁对象时不想降低健康状况,请删除此代码。
现在谈谈这部分问题:
现在问题是我有一个健康脚本所以当玩家 用他的身体接触任何物体他会失去健康
当刚体连接到父对象时,触发器是由所有子对撞机引起的。所以要解决这个问题:
希望这能解决您的问题。