Hello I know that I have the code right. I want to destroy the material when my player goes on them. I don't know why I can't destroy them. I have put only to my materials box collider with X= 1 Y=1 Z=1.I don't understand why I can't destroy them. The material I gave it also as a tag. Instead of my player destroy those material he pass through them..I have a RigidBody on the player.
void OnCollisionEnter ( Collision collision )
{
if ( collision.gameObject.tag == "material" )
{
Destroy ( collision.gameObject );
}
}
You need to debug collisions
try this function and screenshot both results and material gameobject :
void OnCollisionEnter(Collision collision)
{
Debug.Log("all collisions :" + collision.gameObject.name);
if (collision.gameObject.CompareTag("material"))
{
Debug.Log("Material collision :" + collision.gameObject.name);
Destroy(collision.gameObject);
}
}
}
Related
I have written code to heal the player after colliding with the health potion and then destroy the potion game object making it one time use, however, the gameobject does not get destroyed and the player is not healed. Code is as shown below:
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Player" )
{
playerHealthScript.heal();
Destroy(gameObject);
}
}
}
(code below is in a seperate script, used for player health)
public void heal()
{
currentHealth += healingAmount;
currentHealth = Mathf.Clamp(currentHealth, 0, 100);
healthBar.fillAmount = currentHealth / 100f;
}
Before seek for anything, you should Debug.Log("Collision") in your first function, maybe your code is right but the collision isn't detect.
If so, yo could consideer check if the syntax of "Player" is the same as the tag, or maybe your "Player" doesn't have any rigidbody2D ( 2D is important ! ).
I am working on a simple game where the goal is to help a Player catch specific objects with a tag "Present".
After taking care of the models, animations I am now working on the collisions and counting UI.
For the collisions, on my Player Controller (I am using the ThirdPersonUserController from the player Unity Standard Assets - which simplified the whole animation process), I added the following functions:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count == 0)
{
winText.text = "Thanks to you the Christmas will be memorable!";
}
}
However, like this, when the Player hits an object with the tag "Present", even though the counting is working properly, the player disappears.
I have tried to change the OnCollisionEnter to an OnTriggerEnter, like this:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(gameObject);
count = count - 1;
SetCountText();
}
}
However when the Player hits the objects with the tag "Present", they don't disappear.
My Player has a Capsule Collider and a Rigidbody.
The objects with the tag "Present" have a Box Collider and a Rigidbody.
Any guidance on how to make my Player stay in scene while removing the other objetcs and reducing the count is appreciated.
Few things. You are destroying the incorrect game object:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.tag == "Present")
{
Destroy(gameObject); // this is destroying the current gameobject i.e. the player
count = count - 1;
SetCountText();
}
}
Update to:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject); // this is destroying the other gameobject
count = count - 1;
SetCountText();
}
}
Always use CompareTag() which is optimized for performance.
Setting the Collider IsTrigger property will then make use of the OnTriggerEnter events and not the OnCollisionEnter anymore.
On the first physics update where the collision is detected, the
OnCollisionEnter function is called. During updates where contact is
maintained, OnCollisionStay is called and finally, OnCollisionExit
indicates that contact has been broken. Trigger colliders call the
analogous OnTriggerEnter, OnTriggerStay and OnTriggerExit functions.
See the docs
Your present object is using a non-trigger collider, so you should use OnCollisionEnter. The CompareTag call you tried in OnTriggerEnter is preferable. You should use that.
Also, you are currently trying to destroy the gameobject that the ThirdPersonUserController is attached to (gameObject). Instead, destroy the gameobject of the colliding collider (other.gameObject) with Destroy(other.gameObject);:
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Present"))
{
Destroy(other.gameObject);
count = count - 1;
SetCountText();
}
}
So, I'm making a coin system. When a player collides with trigger collider, i want it to disable only the object it collided with.
this.SetActive(false);
Finding object by name
void OnTriggerEnter (Collider other)
{
if (other.tag == "Coin")
{
this.SetActive(false);
}
}
/
You were very close with your original solution, but you may be misunderstanding what is actually happening here. So I've documented my solution to show the difference.
/* The following script is called when a Rigidbody detects a collider that is
* is set to be a trigger. It then passes that collider as a parameter, to this
* function.
*/
void OnTriggerEnter (Collider other)
{
// So here we have the other / coin collider
// If the gameObject that the collider belongs to has a tag of coin
if (other.gameObject.CompareTag("Coin"))
{
// Set the gameObject that the collider belongs to as SetActive(false)
other.gameObject.SetActive(false);
}
}
If you want the coin to be removed from the scene, because you don't expect it to ever be reactivated, then you can amend this code to the following:
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag("Coin"))
{
// Removes the coin from your scene instead
Destroy(other.gameObject);
}
}
I am making a 2D game. I have 2 game objects, a player, and some obstacles and I want the player object to be destroyed on collision. I have added box colliders to both the objects as well as tags but the collision is not taking place as there are no log messages in the console.
void OnCollisionEnter2D (Collision2D col)
{
Debug.Log("collision name = " + col.gameObject.name);
if (col.gameObject.tag == "cow") {
Destroy(gameObject);
}
}
As we discussed in comments:
Add Rigidbody2D component to at least one of colliding objects.
I have a collision script that's attached to my player (mainCamera) and bullet prefab.
void onCollisionEnter(Collision collision)
{
Debug.Log("Collision running");
if(collision.gameObject.tag == "Enemy" && this.gameObject.tag == "MainCamera")
{
Debug.Log("Hit Enemy");
if (Controller.Health > 0)
{
Controller.Health -= .2f;
}
} else if (collision.gameObject.tag == "Scenary" && this.gameObject.tag == "Bullet")
{
Debug.Log("Bullet hits Scenary");
Destroy(this.gameObject);
} else if (collision.gameObject.tag == "Enemy" && this.gameObject.tag == "Bullet")
{
Debug.Log("Bullet hits Enemy");
Destroy(this.gameObject);
EnemyScript.Health-=.2f;
}
}
None of my debug logs are being set off. I have proper tags for all of the objects, a rigidbody and sphere collider on the sphere bullet, a sphere collider on the camera, mesh colliders on scenary, and box collider on the enemy which is a cube. None of my is Triggers are ticked. The bullet also has gravity, and it falls after a certain point. It can collide with the box and stop; however when it hits the wall plane or the floor plane, the bullet falls or goes through sometimes, but also sometimes works and hits the wall and stops or hits the floor and stays there until it gets destroyed. Not entirely sure where the problem is.
The problem with the wall or floor is that unity check for physics in fixed Updates , so if you don't put your moving , etc logic in fixed updates this problem occurs.
also try this , add a physic material to your bullet.