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

Turret not spawning fireball via instantiation in Unity 2d?

Discussion in '2D' started by Blinking_Nora, Dec 18, 2015.

  1. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    (I got it working in the end! This thread can be closed now!!)
     
    Last edited: Dec 23, 2015
  2. Kurius

    Kurius

    Joined:
    Sep 29, 2013
    Posts:
    412
    Did you make sure that you checked the box on the Collider of the cone so that the Collider is a "Trigger"? Did you add a tag named "Player" to your player gameObject?
     
  3. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    Yes I have beforehand and still am having the problem. :/

    Thanks for the reply though!!
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Firstly, please use code tags - it makes it way easier to read code.

    Secondly,Mohave you got the latest patch that was just released? There was an issue with 2d triggers & collisions that is being fixed in that patch. This may or may not be your problem but is the easiest to check.
     
  5. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    My apologies, I've fixed up the code after looking up code tags and I agree it does look much. MUCH neater!

    Regarding the latest Unity update: I installed it, but it still doesn't work unfortunately. All it really did was mean that from the version .2.2 I had I was able to optimise and change some of my scripts as they had, I guess newer lines of code to use in replacement that were more optimised.
     
  6. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Put a debug log into your detection part of the script to see if it is being triggered.
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Also, what are you doing with the bool called awake? You are setting it to true or false but I can't see where you are actually using the bool
     
  8. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    Sorry for the late response, but I tried putting 'Debug.Log;' into the code where the attacking function is, however it came up with an error in the console. I know for a fact it wasn't implemented correctly, but after searching online I'm struggling on how to use it.

    The awake variable is also used to play different animations, where when the player approaches from a certain distance away, it plays the 'waking up' animation. Then when they get closer and touch the colliders either side, it's supposed to fire at the player.

    I'd really like to wrap my head around this and find out why it's not working as intended, as this is a great learning experience!
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Ok, assuming you don't have a deadline for this & it's a passion project, beak it up into logical steps rather than trying to understand it all at once.
    1. Detect the player
    2. Rise up
    3. Shoot the player
    Are basically the high level steps you want, so what do you need?
    1. Has it detected the player? Yes or no? That sounds like a bool
    2. How do you detect him? That sounds like a trigger collider that changes the bool
    3. Rising up sounds like your animation that is dependent on the detection bools setting
    4. Shoot the player, that sounds like it can only shoot if the player has been detected & the turret is up & the player is close enough to shoot Since the turret is only up when the player is detected, but there may be some turrets that have detected the player (& can shoot) but others that haven't (& can't shoot) you probably need to base the shooting on the turrets own bool for whether it is awake or not.
    5. That means that the bool for whether the player has been detected needs to be specific to each turret & not a global setting.
    6. Now, the turret can only shoot if the player is closer I.e. It can't shoot once it detects the player, the player has to awaken the turret & move close enough so this sounds like another trigger collider & bool setting.

    Working bit by bit, & using debug.log to see what is happening in the console, get each but of logic working. That way when something doesn't work you will know which step broke it. Since you have the code there just comment out the bits you aren't up to testing yet.

    This is a really slow way to do it, & may end up with code that isn't the most efficient if you were making a massive game, but it lets you understand each bit you are working on. But, this is just the way I work as I'm still trying to learn as well.
     
    theANMATOR2b likes this.
  10. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    Well, it is kinda' deadlined (but not for over three weeks still), since it's a college task where we make a platformer game in Unity, and instantiation is something that we should include. But at the same time I'm pretty interested in programming and understanding how/why the code I have works, and debugging the code you have is something that I feel I should know how to do.

    Also, thanks to your excellent breakdown of debugging (I used the code Debug.Log ("TEST"); as I forgot about including text earlier haha, and how it only triggers within certain parameters and the text is just to show it works), I've concluded that the problem lies somewhere within the attacking portion of the code, near the bottom of the second script I posted. As it registers a collision with the collision cones in the other script just fine, and also when the character gets within a certain distance when it wakes up!

    This means that somewhere between colliding with the turret's 'line of sight', and the instantiation of a fireball, something is going wrong.

    (I also really appreciate all the help you're offering here. I'm really grateful for it all!!)
     
    Last edited: Dec 20, 2015
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Good work. So now you know where it is happening you can try using break points if you want to pause the program & step into the script but first I'd just visually look at your scripts & work out what is happening.

    Looking at the script, you seem to be requiring a bool for shooting right but I can't see where you are setting that bool which means if you never set it to true it will never meet the criteria of your if statement.

    Basically, whenever an action is dependent on certain conditions & it isn't working I always check the if statement conditions. Break points are useful if a visual check doesn't reveal it as it pauses the code & you can step into it. Hovering the mouse over those values then shows you what is being passed across & you will be able to see if any of them are wrong.
     
  12. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    Oh man, you're onto something there! I don't think I do reference an attackingRight bool, and never do much else with it! I edited the code a bit and added another bool in my block of variables of attackingRight, which defaults to true. But now I'd have to implement something like that into my attacking code, however I'm not entirely sure where/how to put it and in what context...

    However, using break points (thanks again for another great tip!), I'm struggling to manage with them, but from what I've found the scripts not not be able to reference eachother very well, as none of the attack code is simply running when it's supposed to. :/

    Gosh, this is starting to become more and more of a dilemma haha.
     
  13. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If you can, put this aside & do (not just watch) the unity space shooter tutorial. It will take you through a lot of this & explain what is happening & why. It may save you time in the long run.

    As a quick test, I think your shoot script is checking for looking left whilst your detection script is checking for looking right or vice versa.

    Ps, I'm still fairly new myself so I'm bad at giving definitive answers but because I'm new most of my code fails so I'm getting good at spending hours & hours trying to nut out why I broke my code which is why I can't tell you exactly what is wrong but can give an idea of where to look.
     
  14. Blinking_Nora

    Blinking_Nora

    Joined:
    Dec 17, 2015
    Posts:
    11
    The left and rights being different doesn't change much sadly, as the left defaults to false and right defaults to true anyway, so the issue isn't there.

    And I may have to check out that space shooter tutorial, but have enough on my plate making a platformer game as well as a space shooter, so may not be able to do as much as I should! I want to thank you again for all the help though, even though you say you're new you're pretty well informed! I'm just baffled at how this code really should be running fine, but isn't and it's stressing me out haha.