Bullet doesnt dissapear when hittign a collision unity 2d - unity3d

enter image description herei am making a script where the player can shoot in my unity game. This works but the bullet doesnt go away when hitting a collision. Can anyone help me with this problem? this is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BulletDie : MonoBehaviour
{
public GameObject diePEffect;
public float dieTime;
// Start is called before the first frame update
void Start()
{
StartCoroutine(Timer());
}
// Update is called once per frame
void Update()
{
}
private void OncollisionEnter2D(Collision2D collision)
{
GameObject collisionGameObject = collision.gameObject;
if (collisionGameObject.name != "Player")
{
Die();
}
}
IEnumerator Timer()
{
yield return new WaitForSeconds(dieTime);
Die();
}
void Die()
{
if (diePEffect != null)
{
Instantiate(diePEffect, transform.position, Quaternion.identity);
}
Destroy(gameObject);
}
}

Related

Change text from instantiate script object

I am instantiating a object (Prefab) when I press a button and I want to change a text like a Score from its instantiating object script but when I try to add the text to prefab in the inspector I can't.
this is the script prefab object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BulletScript : MonoBehaviour
{
public float speed;
public Text points;
private Rigidbody2D rigidBody2D;
// Start is called before the first frame update
void Start()
{
rigidBody2D = GetComponent<Rigidbody2D>();
}
private void FixedUpdate()
{
rigidBody2D.velocity = Vector2.left * speed;
}
private void OnTriggerEnter2D(Collider2D collision)
{
Player player = collision.GetComponent<Player>();
BlueShieldScript blueShieldScript = collision.GetComponent<BlueShieldScript>();
RedShieldScript redShieldScript = collision.GetComponent<RedShieldScript>();
if (player != null && gameObject.name != "bullet(Clone)")
{
player.Hit();
Destroy(gameObject);
}
if(blueShieldScript != null && gameObject.name == "bulletBlue(Clone)")
{
blueShieldScript.Hit();
points.text = "1";
Destroy(gameObject);
}
if(redShieldScript != null && gameObject.name == "bulletRed(Clone)")
{
redShieldScript.Hit();
Destroy(gameObject);
}
Destroy(gameObject, 5);
}
}

explosion animation not in the right place when it is played

I was making an enemy death animation on unity(again) when it did not want to work, now when I say it did not want to work I mean it did not want to work in the position it is supposed to be in(which is where the enemy is)
public class EnemyController : MonoBehaviour
{
public int health = 3;
public GameObject explosion;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void TakeDamage()
{
health--;
if(health <= 0)
{
Destroy(gameObject);
Instantiate(explosion, transform.position, transform.rotation);
}
}
}
You're using the transform of an object that you have already destroyed, as transform is a property of gameObject.
Try instantiating the Explosion prefab before calling Destroy(gameobject).
public void TakeDamage()
{
health--;
if (health <= 0)
{
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}

col.gameObject.CompareTag not working! Debug message wont pop up when I collide with a gameobject with the tag "Enemy"

My collision detection is not working! Debug message wont pop up when I collide with a game object with the tag "Enemy". Does anyone know what the issue is? Please help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.TextMeshPro;
using TMPro;
public class PlayerController : MonoBehaviour
{
public float speed = 3;
public float jumpPower = 1.5f;
private Rigidbody2D rigidBody;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.A))
transform.Translate(-.1f, 0.0f, 0.0f);
if(Input.GetKey(KeyCode.D))
transform.Translate(.1f, 0.0f, 0.0f);
}
**void OnCollisionEnter(Collision col){
if(col.gameObject.CompareTag("Enemy")){
Debug.Log("CONTACT");**
}
}
}
Not sure if this already solves your issue but:
The issue might be caused by you moving an Object with a Rigidbody2D component by using the transform component. This breakes the physics and collisions might not get called.
Rather always go through the Rigidbody2D component and use Rigidbody2D.MovePosition in FixedUpdate like
bool keyA;
bool keyD;
void Update()
{
if(Input.GetKey(KeyCode.A)) keyA = true;
else if(Input.GetKey(KeyCode.D)) keyD = true;
}
private void FixedUpdate ()
{
if(keyA)
{
keyA = false;
rigodBody.MovePosition(rigiBody.position - Vector3.right * 0.1f);
}
else if(keyD)
{
keyD = false;
rigidBody.MovePosition(rigidBody.position + Vector2.right * 0.1f);
}
}
In general your Rigidbody should be isKinematic in order to work
Note: MovePosition is intended for use with kinematic rigidbodies.

Particles system falls down

I made a fps with a gun and it has a particles system as effect for the gun shot. The problem is that the effect multiply and falls down. I don't know if I can control this from the effect inspector or the script of spawning projectiles
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpawnProjectiles : MonoBehaviour
{
public GameObject firePoint;
public List<GameObject> vfx = new List<GameObject>();
private GameObject effectToSpawn;
public Button button;
// Start is called before the first frame update
void Start()
{
effectToSpawn = vfx[0];
button.onClick.AddListener(TaskOnClick);
}
// Update is called once per frame
void Update()
{
}
void SpawnVFX ()
{
GameObject vfx;
if(firePoint != null)
{
vfx = Instantiate(effectToSpawn, firePoint.transform.position, Quaternion.identity);
}else
{
Debug.Log("NoFirePoint");
}
}
void TaskOnClick()
{
SpawnVFX();
}
}

How to make an enemy stop when he enter the required distance to shoot

The problem that i have is that when the enemy enters the range to shoot it stops and never follows the player again even if the player gets out of the shooting range. To detect if the player entered the shooting range I have made a sphere collider for the enemy.
using UnityEngine;
using System.Collections;
public class EnemyWithRifleMovement : MonoBehaviour {
private GameObject player;
private NavMeshAgent nav;
private bool playerInRange = false;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
player = GameObject.FindGameObjectWithTag("Player").gameObject;
nav = GetComponent<NavMeshAgent>();
}
void Update()
{
if(playerInRange)
{
nav.SetDestination(transform.position);
}
else if(playerInRange == false)
nav.SetDestination(player.transform.position);
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = true;
}
}
void OnTriggetExit(Collider other)
{
if(other.gameObject.CompareTag("Player"))
{
playerInRange = false;
}
}
}
Your code looks all fine, except there is one typo. Replace OnTriggetExit with OnTriggerExit, and it should work!
Also, in my opinion it is easier to get the distance between the enemy and the player by doing playerInRange = (Vector3.Distance(transform.position, player) <= range.