- Home /
The question is answered, right answer was accepted
Why does my FPSController destroy when I collide?
When my player collides with the r_pickup it destroys the fps controller, and i have no idea why. Sorry if its a easy fix, still a beginner :).
BTW it also does this when i use OnCollisionEnter()
using UnityEngine;
public class AimAnim : MonoBehaviour {
public static AimAnim aimAnimInstance;
public AudioClip pickSound;
bool isPickedUp = false;
bool IsAiming = false;
bool hasAmmo = true;
bool canReload = false;
bool isReloading = false;
bool isDead = false;
public AudioClip shootSound;
public GameObject bullet;
public GameObject spawnLocation;
public int Health = 100;
public int Ammo = 1; //This is the ammo in a single clip
public int MaxAmmo = 3; //this variable will be adjusted for each gun ammount of ammo in each clip all together
public float reloadAnimTime; //The actual animation time for each aim for a gun will be the float number
public float reloadTime; //This number is the same as reloadanimtime but its there so you can set animtime back to its original numb
void Awake ()
{
aimAnimInstance = this;
}
void Update ()
{
Aim ();
Shoot ();
Reload ();
Die ();
}
void Aim ()
{
if ((Input.GetMouseButtonDown(1)) && (IsAiming == false) && (isReloading == false))
{
GetComponentInChildren<Animation> ().Play ("Rocket_Aim");
IsAiming = true;
}
else if ((Input.GetMouseButtonDown(1)) && (IsAiming) && (isReloading == false))
{
GetComponentInChildren<Animation> ().Play ("Rocket_AimOut");
IsAiming = false;
}
}
void Shoot ()
{
if (Ammo > 0) {
hasAmmo = true;
}
else
{
hasAmmo = false;
}
if (MaxAmmo > 0)
{
if ((Input.GetMouseButtonDown (0)) && (hasAmmo) && (isReloading == false))
{
Instantiate (bullet, spawnLocation.transform.position, spawnLocation.transform.rotation);
Ammo--;
MaxAmmo--;
GetComponent<AudioSource>().PlayOneShot (shootSound, 1f);
GetComponentInChildren<Animation> ().Play ("Rocket_Recoil");
}
}
}
void Reload ()
{
if (isReloading)
{
reloadAnimTime -= 1 * Time.deltaTime;
if (reloadAnimTime <= 0)
{
isReloading = false;
reloadAnimTime = reloadTime;
}
}
if ((Ammo < 1) && (MaxAmmo > 0))
{
canReload = true;
if ((Input.GetKeyDown (KeyCode.R)) && (canReload))
{
GetComponentInChildren<Animation> ().Play ("Rocket_Reload");
IsAiming = false;
isReloading = true;
Ammo++;
}
}
}
void Die ()
{
if (Health <= 0)
{
isDead = true;
print ("Player died");
}
if (isDead)
{
Application.LoadLevel ("Die_Scene");
}
}
void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "R_Pickup")
{
isPickedUp = true;
if (isPickedUp)
{
Destroy (gameObject, 0.7f);
GetComponent<AudioSource> ().PlayOneShot (pickSound, 1f);
AimAnim.aimAnimInstance.MaxAmmo++;
}
else
{
isPickedUp = false;
}
}
}
}
Answer by phxvyper · Mar 31, 2016 at 07:27 PM
In your OnTriggerEnter function you call Destroy(gameObject, 0.7f);
which is the same thing as calling Destroy(this.gameObject, 0.7f);
. You're telling Unity to Destroy the controller's game object, not the object tagged R_Pickup.
If you want to destroy the R_Pickup object, then do:
Destroy(other.transform.parent.gameObject, 0.7f);
Follow this Question
Related Questions
destroy a non-trigger object hitting a trigger object? 1 Answer
Destroying Multiple Game Objects w/ OnTriggerEnter 1 Answer
How do I make this switch destroy an object? 1 Answer
OnTriggerEnter Influencing all GameObjects of the Same Type 2 Answers
Resources.load loads twice ( onTriggerenter->load-another->destroy-self ) 1 Answer