Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Make a block kill player if touched?

Discussion in 'Scripting' started by JackThe12YearOldDeveloper, Dec 31, 2013.

  1. JackThe12YearOldDeveloper

    JackThe12YearOldDeveloper

    Joined:
    Dec 28, 2013
    Posts:
    7
    How would I make a block kill the player if he runs into it? Thanks!
     
  2. HungrySnake

    HungrySnake

    Joined:
    Dec 27, 2013
    Posts:
    16
    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.

    Code (csharp):
    1. function OnControllerColliderHit ( pHit : ControllerColliderHit ) {
    2.   if ( pHit.gameObject.tag == "Block" ) {
    3.       Destroy (this); // Destroy player, it's an example action, I'm sure you want other actions here
    4.   }
    5. }
     
  3. Euranium_

    Euranium_

    Joined:
    Dec 9, 2017
    Posts:
    10
  4. Homicide

    Homicide

    Joined:
    Oct 11, 2012
    Posts:
    637
    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.
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536

    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)
     
    Homicide likes this.
  6. KingTyuio

    KingTyuio

    Joined:
    Jul 8, 2020
    Posts:
    3
    that is in c # you say it like c sharp
     
  7. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    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.
     
    PraetorBlue likes this.
  8. Amelia_Uchiha

    Amelia_Uchiha

    Joined:
    Jul 8, 2021
    Posts:
    1
    Yeah, the code thing didn't work for me.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    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:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/