How would I make a block kill the player if he runs into it? Thanks!
Use OnControllerColliderHit, this is called whenever the player (controller) collides with another object while it’s moving, so that’s what you want. OnControllerColliderHit has an argument for ControllerColliderHit, it basically returns information about the object the player is colliding with.
function OnControllerColliderHit ( pHit : ControllerColliderHit ) {
if ( pHit.gameObject.tag == "Block" ) {
Destroy (this); // Destroy player, it's an example action, I'm sure you want other actions here
}
}
@HungrySnake Can that code in C# please?
Of course, this is only useful if using a CharacterController Component.
But there is many ways to destroy an object through interaction, with or withput the use of CharacterController
OnTriggerEnter()
OnCollisionEnter()
Both are good alternate methods and can accomplish what you desire.
this, you would first want to decide who should declare the hit(i.e, does the player tell by itself if it’s touching X he should die or does thing X tell the player to die if he touches him)
Do take note:Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached (from the unity docs)
that is in c # you say it like c sharp
This necro is a real head-shaker. Please take a look at the forum guidelines and don’t necro old threads unless you have something meaningful to add.
And anyway, the code he was quoting wasn’t C#. It was UnityScript.
Yeah, the code thing didn’t work for me.
Don’t reply to eight year old threads with content-less complaints please. It’s against forum rules.
Instead, if you actually want to make progress, start your own post… it’s FREE!
When you post, keep this in mind:
How to report your problem productively in the Unity3D forums:
How to understand compiler and other errors and even fix them yourself:
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: https://discussions.unity.com/t/481379