I have the code for destroying a Cube GameObject when it collides with the Terrain. However, Im not sure how I would then after instantiate a New Sphere GameObject in its place after the cube is destroyed.
This is the current code:
{
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag != "Destroy")
{
Destroy (gameObject);
}
}
}
1) Attach this script to your terrain game object and not the cube.
2) Add a new tag in the editor for cube objects (e.g cube).
3) Create a new sphere prefab instance that you can access through the script containing the OnCollisionEnter() event.
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag == "Cube")
{
//store the transform component of the gameobject to be destroyed.
var transf = collision.gameObject.transform;
//Destroy the collided gameobject
DestroyImmediate(gameObject);
//Instantiate in the position and rotation of the destroyed object.
Instantiate(sphere, transf.position, transf.rotation);
}
}
Related
I'm a noob trying to make a game like this https://youtu.be/qxwO1wyz50w?t=37 , but i cannot figure out how to add collision to my line renderers. Would a line renderer be the way to go about this or is there a better way?
Here is my code so far (messy I know im new to programming as well )
public GameObject crossHair;
public LineRenderer line;
public Transform startPoint;
public GameObject lazer;
void Start()
{
Cursor.visible = false;
}
void Update()
{
crossHair.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetKeyDown(KeyCode.Mouse0))
{
LineSpawner();
startPoint.position = crossHair.transform.position;
}
}
void LineSpawner()
{
Vector2 shootDir = crossHair.transform.position - startPoint.transform.position;
GameObject lazerInstance = Instantiate(lazer);
line = lazerInstance.GetComponent<LineRenderer>();
line.SetPosition(0, startPoint.transform.position);
line.SetPosition(1, crossHair.transform.position);
Physics2D.Raycast(startPoint.transform.position, shootDir);
Debug.DrawRay(startPoint.transform.position, shootDir, Color.red);
}
Unfortunately, you cannot add a collider component to line renderers but a way around this is to create another gameobject that is invisible and make it a child of the line itself. What you then want to do is use the invoke("targetFunctionName", amountOfSeconds) method to destroy the collider gameobject after the player has hit it, this is so the game doesn't lag for those who are doing well while playing it. What you then want to do is add a physics material to the game object and set its bounciness property to max(which I think is 1). Do the same for the player's gameobject and you're done.
Why my GameObject named "pipo" is not destroyed
This is my script:
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.name == "pipo")
{
Destroy(other.gameObject.transform.parent.gameObject);
}
}
Try to change your Code a little bit, first you should generally use CompareTag() which gives Error Messages when the given Tag doesn't exist.
After that you can add a check to see if the gameobject has a parent and depending on that destroy its parent or itself.
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("pipo")){
return;
}
if(other.gameObject.transform.parent) {
Destroy (other.gameObject.transform.parent.gameObject);
}
else {
Destroy ( other.gameObject);
}
}
When the object still doesn't get destroyed, you need to make sure that:
The Gameobject you want to destroy has the tag called "pipo"
The GameObject where this script lies has IsTrigger enabled as well as a Collider
The "pip" GameObject has a Colllider and is not set to IsTrigger
Both objects have a Rigidbody component attached to them
Collider is attached to the "pipo" GameObject and not its parent
I am trying to have my player ignore the collision with an edge collider on a platform i have.
Here's the script that I have added to the player
public class TestMovement : MonoBehaviour
{
public Rigidbody2D ball;
private GameObject purplePlat1;
private GameObject player;
// Start is called before the first frame update
void Start()
{
purplePlat1 = GameObject.Find("purple_plat");
player = GameObject.Find("circle-png-44659");
ball = GetComponent<Rigidbody2D>();
ball.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
Debug.Log("start");
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D collision)
{
Physics2D.IgnoreCollision(purplePlat1.GetComponent<EdgeCollider2D>
(), GetComponent<CircleCollider2D>());
Debug.Log("collision");
}
}
The ball is still hitting the platform. I have confirmed that the oncollisionenter method is firing.
You can use the layer system of Unity to avoid collisions between both. Set a layer for a player and another for the edge and untick the collision between them.
What you can do is create a layer mask for the different type of game objects. Then, open your Physics2D settings.
On the bottom part, you can see a matrix of physics objects that can collide to one another. Just uncheck which layer should not collide with the other.
So I have a sprite and it's name is "princess" and when a cube touches my sprite
I want the princess to be destroyed. I added a rigidbody, and a box collider, but for some reason the cube just goes through the princess sprite.
The cube is generated with code so it's name is "Cube" according to the hierachy so I wrote this code
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "Cube")
{
Destroy(this.gameObject);
}
}
I think Destroy(this.gameObject) would destroy the princess, but they're not even colliding.
Any ideas?
Here is what the "game" looks like.
Game
Check if you checked the "is trigger" inside the box colliders.
You should use tag because if you spawn multiple cubes the will have names like "Cube (1)" and so on.
You need to use collider2d
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Cube")
{
Destroy(this.gameObject);
}
}
both of your object needs to have box collider2d and rigidbody2d and not set to "is trigger". example:
Destroy(this.gameObject) is working for own object where
collision.gameObject.setActive("False") is working for collide object
This code is working on my game. where "Player" is my Sprite tag. And make sure 'Is Trigger' is checked for your Cube
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player")
{
Destroy(gameObject);
}
}
OR alternatively in your case below code you could use in Cube script then might be work
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "princess")
{
collision.gameObject.setActive("False")
}
}
So I'm trying to get my vehicle to play an explosion when colliding with an enemy vehicle and have succeeded in making the explosion appear but when doing so it is cloned outside of the parent object and gets left behind when collision happens at a speed so the actual player car doesn't look like its exploding but instead travels past the explosion prefab. Any ideas on how to fix this would be much appreciated. Thanks
void OnTriggerEnter(Collider col)
{
if (col.tag == "Enemy")
{
PlayExplosion();
}
}
void PlayExplosion()
{
GameObject explosion = (GameObject)Instantiate(ExplosionGo);
//set the position of the explosion
explosion.transform.position = transform.position;
}
You need to either manually set the parent:
explosion.transform.parent = transform;
Or supply a parent transform in the Instantiate parameters:
GameObject explosion = (GameObject)Instantiate(ExplosionGo, transform);
You need to set explosion transform a child of your car transform/ Try this:
void PlayExplosion()
{
GameObject explosion = (GameObject)Instantiate(ExplosionGo);
explosion.transform.SetParent(transform, false);
}