Search Unity

Ball hitting wall its hp will decrease

Discussion in 'Scripting' started by Valery_RU85, Nov 21, 2017.

  1. Valery_RU85

    Valery_RU85

    Joined:
    Nov 21, 2017
    Posts:
    2
    Hi

    1. I would like to know how to code - when the ball hits a wall its hp will decrease
    2. I'd also like to know if there's some1 willing to help me mutliple times since coding is very new to me and im so noob.
    I do watch the videos on youtube and read on unity

    Help is very much appreciated and thankfull in advance.

    Best regards

    Valéry
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Start some reading here, especially the "Script actions taken on collision" section:

    https://docs.unity3d.com/Manual/CollidersOverview.html

    So most likely you'll want to use the OnCollisionEnter method which is automatically called on a collision. Then you'll probably want to verify that what you have collided with is a wall, probably by checking for a wall script or something you have on your wall gameobjects that you can use to identify them. Then if it is confirmed you've hit a wall, then you decrease your HP. Note that OnCollisionEnter is called for collisions with anything, even things you won't necessarily want to have HP decrease, which is why you need to verify it is a wall first.

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter.html
    https://docs.unity3d.com/ScriptReference/Collision.html
     
    Last edited: Nov 21, 2017
    Valery_RU85 likes this.
  3. Valery_RU85

    Valery_RU85

    Joined:
    Nov 21, 2017
    Posts:
    2

    Thank you very much! I'll try to figure out myself coz already hav 3 questions after reading your post...

    Also atm I'm watching this: