Search Unity

Make text appear after collision

Discussion in '2D' started by jamfu123, Dec 23, 2018.

Thread Status:
Not open for further replies.
  1. jamfu123

    jamfu123

    Joined:
    Jul 30, 2018
    Posts:
    16
    Hi, does anyone know how to achieve this ?

    I want my player to pickup a powerup which will then enable them to fire. Ive already got the scripting down for shooting, just want the script to activate after the item pickup.
     
  2. Thimble2600

    Thimble2600

    Joined:
    Nov 27, 2015
    Posts:
    165
    Make a text UI gameObject and set it as a prefab, then instantiate it when needed. Set its parent to the canvas and give it a script that destroys it after a set duration.

    Have the player character's script subscribe to an event onPickUp which gets called when the player picks up any kind of power up. Pass the power up name or value representing its type to the subscribed object, then I guess modify the player characters fire rate or what-not.
     
    jamfu123 likes this.
  3. jamfu123

    jamfu123

    Joined:
    Jul 30, 2018
    Posts:
    16
    Thanks for the reply, im still a beginner at coding so i will try and figure this out.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well you can put the text object into a Text variable and set its enabled to false from the start. And when your player collides, set the enabled to true.
    Code (CSharp):
    1. [SerializeField]
    2. private Text yourText; // Insert your text object inside unity inspector
    3.  
    4. void Start()
    5. {
    6.     yourText.enabled = false; // You may need to use .SetActive(false);
    7. }
    8.  
    9.  
    10. // Assuming you're using a 2D platform
    11. void OnCollisionEnter2D(Collision2D collision)
    12. {
    13.    if(collision.gameObject.tag == "Player")
    14. {
    15. // This is where you make your text object appear on screen
    16.     yourText.enabled = true; // May need to use .SetActive(true);
    17. }
    18. }
    19.  
    20. void OnCollisionExit2D(Collision2D collision)
    21. {
    22.      // Here is where you make the text disappear off screen
    23.       if(collision.gameObject.tag == "Player")
    24. {
    25.    yourText.enabled = false; // May need to use .SetActive(false);
    26. }
    27. }
    Just keep in mind that if you have a script on your text object and you use .SetActive(true/false) that you'll cancel the attached scripts which is why I think it would be better to use .enabled = true/false instead. However, under certain circumstances you'll have to use .SetActive(). So it's good to familiarize yourself with both.

    Good luck to ya! :)

    Oh almost forgot:
    Code (CSharp):
    1. using UnityEngine.UI;
    Make sure to add the namespace or the Text key will not work for your variable.
     
    Advayamahanot and yektasarioglu like this.
  5. jamfu123

    jamfu123

    Joined:
    Jul 30, 2018
    Posts:
    16
    Thanks for the response! alright got it!
     
  6. jamfu123

    jamfu123

    Joined:
    Jul 30, 2018
    Posts:
    16
    Also i have another issue, my player is falling through the ground when i have a box collider 2D on it and the platform it is standing on.

    Is trigger is set to ON for the player, is it the gravity scale from the rigidbody2d causing this issue ?
     
  7. jamfu123

    jamfu123

    Joined:
    Jul 30, 2018
    Posts:
    16
    Ah i got it work now! I was scratching my head on this one.

    The player needs is trigger off and also the platform
     
  8. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You can set your gravity scale on your rigidbody component to 0 and when your player jumps, set it to whatever you need until the player's collider touches the ground. And yes, mi amigo, I remember when I first started coding and how much this was a pain in my butt lol
    Things that you should look into are OnTriggerEnter, OnTriggerStay and same for OnCollisionEnter and OnCollisionStay(Add 2D if you're programming in 2D and using 2D colliders. 2D will let you create 3D objects and use them, but a 2D collider doesn't react.)

    There's also OnGUI function that might spark your interest? The hardest part about learning the C# API language is discovering the built in functions that make things soooo much more simple!

    I'm glad to have been of help. I'll help you with any questions you have. Merry Xmas! :)
     
    Advayamahanot and jamfu123 like this.
  9. jamfu123

    jamfu123

    Joined:
    Jul 30, 2018
    Posts:
    16
    Awesome man! will do! yes starting out ive been scratching my head alot haha but its expected.

    Im making progress though day by day. Another question i have a particle system setup so that when my enemy takes damage the effect will trigger. Ive got that working fine, the only problem is how do i control the script so that it only emits on contact and not at the start of the game, ive tried turning off play on awake but then my effect doesnt trigger at all


    Merry Christmas!
     
Thread Status:
Not open for further replies.