How to transform position from one object to the current player position? - c#

So i have this little particlesystem attached on my player so if he dies he explodes. But i cant simply attach the particlesystem under the player because if i destroy my player the childs of the gameobjects get destroyed as well. The animation runs if he dies but not on his current spot so some ideas for that? Maybe to transform the position to the current position of the player while he dies?
Here my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerdeath : MonoBehaviour
{
public ParticleSystem death_explosion;
// Start is called before the first frame update
void Start()
{
death_explosion.Stop();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "deathcube")
Destroy(gameObject);
Debug.Log("collision detected");
death_explosion.Play();
}
}

What you could do is create a prefab of the particle object and reference it inside your script like so:
public ParticleSystem death_explosion_Prefab;
And instead of attaching it to the Player as a child, instantiate it on collision:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "deathcube")
{
Debug.Log("collision detected");
Instantiate(death_explosion_Prefab, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}

got it :) --> add death_explosion.transform.position = GameObject.Find("player").transform.position;

Related

When my game in unity starts, my character collides with objects it isn't touching

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickup : MonoBehaviour
{
public GameObject _Pickaxe;
// Start is called before the first frame update
void Start()
{
_Pickaxe = GameObject.Find("Pickaxe");
}
// Update is called once per frame
void Update()
{
}
void DestroyGameObject()
{
Destroy(gameObject);
}
private void OnTriggerEnter(Collider collision)
{
Debug.Log("Collided with +" + collision.gameObject.name);
if(collision.gameObject.name == "Pickaxe" );
{
Debug.Log("Touched pick");
}
}
}
In my script it logs whatever the character touches. In my scene i have a object placed about 10m away from where the player capsule spawns, somehow it collides with it before you can even move.
Your code have an error. It logs "Touched pick" for every collision. To fix it, your should remove the semicolon in this line if(collision.gameObject.name == "Pickaxe" );

Bullet not destroying

I want my bullet to get destroyed OnTrigger, however it doesn't get destroyed but that Debug.Log works fine. I have tried everything such as rewriting the script, replacing it and attaching it over and over again. Could somebody help me?
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public GameObject Bullet;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
{
Debug.Log("I die");
Destroy(gameObject);
}
}
}
You're destroying the Enemy gameObject. To also destroy the bullet, try the code below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public GameObject Bullet;
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Bullet"))
{
Debug.Log("I die");
Destroy(gameObject); // destroying self object (Enemy Object)
Destroy(collision.gameObject); // destroying collided object (Bullet Object)
}
}
}
If you want to destroy the bullet you should pass the bullet object as an argument to the function Destroy(), but you are passing the enemy object.
replace Destory(gameObject) with Destroy(Bullet).
Or if you want to destroy the bullet that get triggered replace it with : Destroy(collision.gameObject)

OnCollisionEnter not detecting

I am trying to make the game go to Dead scene when collided by does not work. And it even does not detect the collsion.
I've attached the script to empty game object that has box collider with is trigger ticked and the player does have a Rigidbody too.
I am not sure what is wrong, please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Health : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void OnCollisionEnter(Collision collision)
{
Debug.Log("dead without if statement");
if (collision.gameObject.tag == "Player")
{
Debug.Log("Dead Mate");
SceneManager.LoadScene("DeadScreen");
}
}
}
"I've attached the script to empty game object that has box collider with is trigger ticked and the player does have a Rigidbody too."
If i understand you correct you enabled trigger on box collider if yes you must implement
void OnTriggerEnter(Collider col)
{
}
Not
public void OnCollisionEnter(Collision collision)
{
}

Object is not triggering collider

Hi my problem is in Unity, I am a beginner in c#, my gameObject is not triggering the collider that is set on the plane of the game, in order for it to reset it's position.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasketballSpawnScript : MonoBehaviour
{
public Transform respawnPoint;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Basketball"))
{
other.gameObject.transform.position = respawnPoint.position;
}
}
}
This script is attached to the plane and the gameobject is tagged with Basketball, when it enters the collider of the floor it should transform it's position to the original position.
I cannot see what is wrong, can I receive some help?
P.S I get this error when other gameobject go through the collider too.
NullReferenceException: Object reference not set to an instance of an object
If using a Transform for spawn point, remember to set the value of it in the inspector menu.
public Transform respawnPoint;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Basketball"))
other.transform.position = respawnPoint.position;
}
else
public Vector3 respawnPoint = Vector3.zero;
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Basketball"))
other.transform.position = respawnPoint;
}
private void OnTriggerEnter(Collider other){
if(other.gameobject.tag=="Basketball"){
other.gameobject.transform.position = respawnPoint;
}
}
I hope it helps you.

Make object that destroy player

I writing simple game on Unity (C#)
I have player and want to make the destroyer, that will destroy player.
I create prefab of destroyer. And next, I create Quad.
I have spawn script:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour {
public GameObject[] obj;
public float spawnMin = 1f;
public float spawnMax = 2f;
// Use this for initialization
void Start () {
Spawn();
}
void Spawn()
{
Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
Invoke("Spawn", Random.Range(spawnMin, spawnMax));
}
}
Also I write DestroyerScript:
using UnityEngine;
using System.Collections;
public class DestroyerScript : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy(other.gameObject);
}
}
}
My destroyer spawning, but when player get it, I don't have Game Over screen.
Add rigid body to both player and "player destroyer" and then set onTriggerEnter on your player destroyer like so:
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
}
For some fine tuning, you can do some checks if the other object is in fact the Destroyer (you can compare the tag or something, I won't go into too much detail now).
EDIT: Uncheck "isTrigger" on your BoxColliders and try this:
void OnCollisionEnter (Collision col)
{
Destroy(col.gameObject);
}

Categories