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

sword script

Discussion in 'Scripting' started by StormGamer, Nov 29, 2011.

  1. StormGamer

    StormGamer

    Joined:
    Oct 26, 2011
    Posts:
    283
    hey guys i am making my game vantrask and i really need help with the sword script
    all i want it to do is when it touches another object it takes health from it when its health reaches 0 the object gets changed into another object

    please can you guys help me?
    thank you
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I'm going to explain to you how you should do it... you should attempt to put the code in place yourself, and if you have trouble come back.

    1. Put a rigidbody and a box collider on your sword. Tick the 'IsTrigger' option
    2. Repeat step one for object you want to hit.
    3. Set a tag on your sword. Make it something obvious like 'Sword' for now.
    4. Create a script and attach it the object you want to be hit.
    5. Add a variable at the top of the script called totalhealth
    6. Inside script, add an OnTriggerEnter event
    7. Inside that function, check the tag of the object transform that hit it. if the tag equals sword, then reduce health.

    Try get that going first..
    7.
     
  3. StormGamer

    StormGamer

    Joined:
    Oct 26, 2011
    Posts:
    283
    thanks for the help but my object still doesn't loose health all it does is to make my character jump when it hits the box with the script any ideia?

    this is my script
    also ridgidbody is disabled becouse if i enable it the sword goes away
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Make sure you have the IsTrigger option ticked, otherwise they will fire off physics events, which would explain the character jumping around.

    You should probably also set the sword parent to the player object

    alos, code should read more like this

    Code (csharp):
    1.  
    2.  
    3. var totalhealth = 100;
    4.  
    5. function OnTriggerEnter(other : Collider){
    6. if(other.tag == "Sword"){
    7. totalhealth -= 50;
    8. }
    9. }
    10.  
    11. function Update(){
    12. if(totalhealth <= 0){
    13. Destroy(gameObject);
    14. }
    15. }
    16.  
    17.  
     
  5. StormGamer

    StormGamer

    Joined:
    Oct 26, 2011
    Posts:
    283
    ty man!
    really helped
     
  6. Darkdragon0011

    Darkdragon0011

    Joined:
    Apr 27, 2015
    Posts:
    35
    Thank you soo much!!! I've been looking for a melee script for a long time!!