Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

TurnBased-Toolkit (TBTK)

Discussion in 'Assets and Asset Store' started by songtan, Aug 11, 2013.

  1. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    Unity automatically makes sounds more quiet the further away the source is from the listener (usually the main camera). This can be adjusted in the audio source. So if the unit with the audio is closer to the listener it will be louder and get quieter as it moves away from the listener.
     
  2. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    so..how do i make it louder?
     
  3. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
  4. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Well if you stick with the current setting (3D sound), it's inevitable that audio from far away will grow faint, to a point where it's inaudible. If you want them to be audible all the time, the best thing would be to use 2D sound. You can do that by decreasing the "Spatial Blend" value on the audio source.
     
  5. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    I usually set the minimum range to be the approximate maximum height for the camera; that way when it is right over a unit, it plays at full volume and then gets quieter as you move the camera away.
     
  6. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    i looked into it in unity..can't find it is there a way to turn the 3D sound into a 2D sound?..or a script that does it?
     
  7. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    Set the "spacial Blend" to 0 for 2d sound and 1 for 3d sound. Sort of explains it in the manual (but not very well really).
     
  8. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    i found a easier way..click the box where it says sound 3D to off and it works!
     
  9. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Hi Sontan,

    I'm working on some minor improvements to the AI in my version. One problem I have is that if the map is congested (such as a large number of enemies in a narrow corridor), the AI will try to move the guys in a random order (with free movement selection mode) and often it will choose a guy in the rear who has nowhere to move first and then not move him at all that turn.

    It would be better if the AI would look at the units closest to the player characters and move them first so that they are then out of the way of the guys behind, who will then have empty tiles to move into.

    I looked at the code in my version where the move order is decided and it's here:

    Code (CSharp):
    1. if(GameControlTB.GetMoveOrder()==_MoveOrder.Free){
    2.             int count=allUnit.Count;
    3.          
    4.             //create a list (1, 2, 3..... ) to the size of the unit, this is for unit shuffling
    5.             List<int> IDList=new List<int>();
    6.             for(int i=0; i<count; i++) IDList.Add(i);
    7.          
    8.             for(int i=0; i<count; i++){
    9.                 //randomly select a unit to move
    10.                 int rand=UnityEngine.Random.Range(0, IDList.Count);  
    11.              
    12.                 int ID=IDList[rand];
    13.                 UnitTB unit=allUnit[ID];
    14.                 //remove the unit from allUnit list so that it wont be selected again
    15.                 IDList.RemoveAt(rand);
    16.              
    17.                 counter+=1;
    18.              
    19.                 //move the unit
    20.                 unitInAction=true;
    I'd like to populate the list of unitID's or reorder them so by the distance to the closest player unit. Then rather than choosing one at random to move, I'd like to pick the unit that is closest to the enemy and move them first.

    Any thoughts on how I could modify this bit of code to do that?
     
  10. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Well it's actually a little more complicated than you think. You will need a sorting algorithm. Rather than modify this, I would just rewrite the whole thing. A very crude implementation would be something like this:
    Code (csharp):
    1.  
    2. List<UnitTB> allHostiles=UnitControl.GetAllHostile(factionID);
    3. List<UnitTB> allUnit=new List<UnitTB>( faction.allUnitList );
    4.  
    5. List<UnitTB> sortedList=new List<UnitTB>();
    6. List<float> sortedDistList=new List<float>();  //the nearest hostile distance corresponding to each unit in sortedList, much be the same length as sortedList
    7.  
    8. for(int i=0; i<allUnit.Count; i++){
    9.    //first get the nearest hostile for this unit
    10.    //loop through all hostile unit, calculate distance and store the nearest
    11.    float nearestHostileDistance=0;
    12.    for(int n=0; n<allHostiles.Count; n++){
    13.      //
    14.    }
    15.  
    16.    //loop through the sorted list, get the highest slot the unit can be inserted at based on the dist
    17.    bool inserted=false;
    18.    for(int n=0; n<sortedDistList.Count; i++){
    19.      if(nearestHostileDistance<sortedDistList[n]){
    20.        sortedDistList.Insert(n, nearestHostileDistance);
    21.        sortedList.Insert(n, allUnit[i]);
    22.        inserted=true;
    23.      }
    24.    }
    25.  
    26.    //if the unit hasn't been inserted into the sortedList, put it at the back of the list
    27.    if(!inserted){
    28.      sortedDistList.Add(nearestHostileDistance);
    29.      sortedList.Add(allUnit[i]);
    30.    }
    31. }
    32.  
    33. //now all the unit has been sorted, we can move them according to the list
    34. for(int i=0; i<sortedList.Count; i++){
    35.  
    Keep in mind this is untested and certainly optimized. If you have lots of unit in the scene, you might want to look for a better sorting algorithm rather than this brute force one.
     
  11. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Thanks Songtan ... appreciate the quick turnaround.

    On another topic, I noticed that (in my version) ... when a unit uses a teleport ability and moves to a tile where they reveal new enemies that were previously hidden (Fog of War enabled), the teleport does not reveal these enemies. So even though the teleporting unit has LOS of the enemy, the enemy are still invisible. They are only revealed if one of the player units then performs a move. Any idea how to fix this?
     
  12. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    I remember that bug. You need to add this line if(onNewPositionE!=null) onNewPositionE(this); at the end of the teleport code. You can find the teleport code in UnitTB.cs. Try to do a search using keyword "teleport". It should be pretty obvious.
     
  13. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    how do i have a unit spawn at a number turn? like unit -> Battleship -> Spawn on spot 22 -> turn 10
     
  14. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    There should be a function - InsertUnit(UnitTB unit, Tile tile, int factionID, int duration) in UnitControl.cs. Basically you pass the unit prefab, tile, factionID and active duration and the function will spawn the unit at the specific tile for the faction. So in your own script, you will need to have a way to specify the unit prefab and factionID. Then you will have to wait for the right time to call the function. You can do so by listening to event GameControl.onNewRoundE. Upon the event is received, you can check GameControl.roundCounter to determine which round is it and if it's the right time to spawn the unit.
     
  15. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Thanks Songtan ... that worked.
     
  16. abysswolf

    abysswolf

    Joined:
    Oct 15, 2012
    Posts:
    62
    would this work for a grand strategy game with a world map and buildings and stuff?
     
  17. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Honestly, it would require a lot of work to add the missing feature, as well as modifying the existing code to fit. The framework by default is meant for turn-based battle on a medium to small scale grid.
     
  18. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    well my game has buildings and the Builder unit can build the light and heavy shipyard
     
  19. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    so..would the new script look something like this?

    InsertUnit(UnitTB patrol boat, Tile 5, int 0, int 999999) GameControl.roundCounter 10
     
  20. Pedro-Oliveira

    Pedro-Oliveira

    Joined:
    Aug 1, 2013
    Posts:
    25
    I am using a square grid and i noticed that the movement range gets cross shaped and vision too, but there is an option (enable diagonal neighbor) that make the movement range get square shaped again. Unfortunately the vision does not and still cross-shaped.There is an option to change that or i will need to change some code? If i do, any suggestions on how to do it?
     
  21. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    one last thing..how do i make the mission end when a unit dies? like unit name -> Battleship -> death -> mission restart
     
  22. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Very strange, my earlier respond to yesterday post has seems be deleted. Very sorry for that.


    Is this the code in your 'script' looks like literally? I'm afraid this is not code at all. Please dont take this the wrong way but do you know anything about coding at all? For your sake, I would strongly recommend you to go and learn some basic coding before trying to tackling this problem.


    I hope you realize the enable diagonal neighbor is very much a unfinished feature? That is why it doesn't work completely. The problem here is the distance calculation between tile always assume that the tiles doesnt connect diagonally. You will need to change that so diagonal neighbour is considered is the enable diagonal neighbor flag is checked. The code for the calculation is in TBTK_Class_Grid.cs, GetDistance(). Basically you will need to do a condition check using if(GridManager.EnableDiagonalNeighbour()) and then perform another set of calculation. Make sense?


    I wonder how much of this will make sense to you. You can call the function GameControl.GameOver(false) to force a game over state. But first you will need a reference to the specific unit (battle ship). Then you can listen to the event Unit.onUnitDestroyedE which pass the destroyed unit. When the event is fired, do a condition check like to see if the destroyed unit match the reference unit, if yes then call GameControl.GameOver(false).
     
  23. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    im...very very very new at codeing im in collage but my codeing clases (well i have one hope i will have more because the professer did not teach the code! he just said "this is a code put it down in unity and fix what errors you get" not explaining the code)


    i think i know what your saying and i have an idea of a code but..im unsure how to turn it into code...
     
  24. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Precisely why you need to learn the basic and do this properly. I could give you a readily made solution but that wont help you in the long run. Ultimately you need to be able to produce on your own. My advise, spend some time to really understand the basic like logic, function, control statement, loop. You can teach yourself if you want to with the amount of coding tutorial out there. Just goggle for it.
     
  25. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Hi Songtan,

    Quick question ... I noticed in my version that when selecting a target tile for an ability that targets units, the tiles with obstacles on them are not selectable. Sometimes a tile with an obstacle on it is the best one to select as it occasionally have enemy units nearby and this will allow the ability to hit the most units (when the ability has an area of effect).

    Any idea on how I could tweak the code to have tiles with obstacles on them as eligible targets?
     
  26. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    il take that..truthfully when looking at scripting i am able to Dissect it so i know what does what
     
  27. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    I dont know which version exactly are you using so I'm answering loosely based on my memory (in other word I might be wrong here). I think the problem is when a tile is set to unwalkable, it's made non-interact-able. You can check in Tile.cs, there should be something like onMouseEnter() or onToucheEnter() which you can verify if this is true. This of course applies for all cases weather you are selecting a unit destination or ability target. You can modify that of course but I suspect it will conflict with a lot of existing stuff.


    I could, but that doesn't mean I'm obliged to. What you are asking me now is to directly work on your game, which is out of the scope of support. And since you say coding is one of the subject in your collage, I suggest you to take the opportunity to learn it properly.
     
  28. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Thanks Songtan ... I'll take a look. You're right in that it might break quite a lot of stuff and might not be worthwhile.
     
  29. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    welp i took your advice and i cant figure out a way to spawn the unit (so it works) that is at my current level of scripting..so i know a way "Around" that so to speek it should still work..

    how aver i was able to make this

    function OnDestroy () {

    Application.LoadLevel (Application.loadedLevel);
    }

    it works perfect! but the new problem now is when the unit dies with this script it is removed befor the death dialog finsihes

    so where in the scripting says how long is the wait to remove the unit from the game? iv been looking and i can't find it
     
  30. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    I'm not sure what do you mean by death dialog, are you referring to the game over window? Well, try replace Application.LoadLevel(); with GameControl.GameOver(); That should trigger the game over state and bring up the main menu, assuming you are using the default UI.

    Note when calling GameControl.GameOver(), you will need to pass the factionID that wins the game. If you are using default setting, the AI faction should have factionID of 1. So it should be GameControl.GameOver(1); If you really want to do a auto restart level, try look into coroutine - http://docs.unity3d.com/Manual/Coroutines.html
     
  31. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    no..the unit death sound

    my unit death sound for this unit is about 15 secs howaver the unit is removed about 2 secs after it dies so the audio does not finish playing and the level resets
     
  32. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    better yet here is a video
     
  33. Pedro-Oliveira

    Pedro-Oliveira

    Joined:
    Aug 1, 2013
    Posts:
    25
    I didn't know. I will need to give up from this feature then. I mean, I could do it but its better to use my efforts on more important things.
    Off-topic : Hope for some feedback on MechCorp soon :) .
     
  34. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    @Pedro Oliveira, I took a look at the distance calculation function and added the component for diagonal neighbour. If you are still interested I can send you the updated script.

    @navy3000, where is that death audio is playing from? The unit audio component? Is it a single 15s long audio clip. As far as I'm can tell the unit should play the whole length of the audio before it destroy itself. From the video, it sound as if the audio has start playing and the unit has waited before it destroy itself. Please send me the clip so I cant test it myself.
     
  35. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    yes it plays from the unit when it is destroyed the audio is 15 secs long http://www.mediafire.com/download/ru6qcc76hxyuel2/New_Compressed_(zipped)_Folder_(2)(2).zip but in the video you can see the problem
     
  36. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Ok I forgot that you are using an old version. Unlike the more up to date version, you will have to manually set the delay for the unit to be destroyed so that the audio and other destroyed effect can be played out. Try look for destroyEffectDuration in UnitEditor and set the value to 15.
     
  37. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    i did that and same thing happen so i was looking around and it seems on line 1261 holds the value so i changed that and it works perfect now!
     
  38. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
    new problem but i think i have a fix..anyways where can i put GetComponent(YourScript).enabled = false; in the GUI end screen so when you click next it also triggers "GetComponent(YourScript).enabled = false;" so the script that restarts the level is set to off?
     
  39. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Well, you can put it down in any of the button click function call in UIGameOver.cs.
     
  40. Pedro-Oliveira

    Pedro-Oliveira

    Joined:
    Aug 1, 2013
    Posts:
    25
    I think won't hurt having it, especially since you already had the trouble to update it. Please do send
    it to my email pedro_henrique_chaves@hotmail.com
    OFF: also I already posted my feedback on the mech corp topic.
     
  41. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Sent!

    And thanks a bunch for you feedback to MechCorp! Really appreciate that.
     
  42. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Going to try to tackle this myself in my version. Do you mind sending me the code you wrote via PM or posting it here? I can use it as a starting point and if I get it working, I'll share it with you.
     
  43. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Hi Songtan,

    I'm just working on updating my ancient version to use Mechanim as your current version does. I took the Mechanim animation code and controller from the latest version into mine. Seems to work nicely.

    However, I want to enhance it a bit and wanted to ask your opinion as I don't know the first thing about Mechanim:

    1 - How could I modify the existing code to have multiple animations of a particular type (idle 1, idle 2, idle 3, etc.) and have one of those animations play when ever the appropriate state is invoked. So at the moment, there is one idle clip that is always played when entering idle state. Is there any way to put in 3 or 4 idles and have the character choose one of them at random?

    2 - Any tips on having it play a particular animation when performing an ability? I have this working in the legacy process but not sure exactly how to do it in Mechanim world with all the process flow charts in the animation controller.

    3 - How do I define the time at which a shoot object is generated in the new Mechanim world? I don't see an easy way to define this ... is it a property defined in the AnimationTB script or somewhere else?
     
  44. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Well,

    1. There two ways to do this as far as I can tell. First is that you create multiple state for each of the animation, each triggered by different conditions (ie four idles state, triggered by 4 different flags idle1, idle2 idle3, idle4). Each time you want to change an idle state, you randomly trigger one of the flag. The other way is to swap out the idle state animation each time you want to switch to another idle animation. The first one is obviously easier in term of coding but require a lot more setting up in the controller. The second one is the other way round. However I must say I'm not sure swapping animation clip in runtime will have any effect on the transition of the animation.

    2. You will have to create another animation state in the animation controller. It can be similar to the one for attack. Considering they are mutually exclusive, it should work just fine. If you haven't figure this out, yes you will have to learn about animation controller to do this I'm afraid.

    3. There is none. At least not in the current version. But I've added this for the next update. You can add a new variable in animation script that hold the delay from when the animation start playing to when the shoot object should be fired. Then in AttackRoutine where you play the animation you can add something like this:

    //play animation
    if(unitAnim!=null){

    unitAnim.Attack();
    yield return new WaitForSeconds(unitAnim.attackDelay);
    }
     
  45. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Thanks Songtan ... got most of that working. Though oddly enough, my old version already had a time delay for the shoot object creation. it's funny how you took that out of the newer versions. :)

    For making multiple animations per state, I'm going with the option of creating multiple states in my Animator state machine. I've created a separate trigger for each and then just randomly send one of those triggers when the character needs to make the appropriate animation.

    However, I'm not sure that will work for the Idle state since it's at the middle of all those states. Having multiple Idle states would probably complicate the whole thing a lot. I wonder whether it would make more sense for the idle animation state to go with a code-based approach where I switch out the animation clip for the one idle state. Any idea how to do this? I'm going through the API for Mechanim animator and it doesn't seem obvious.
     
  46. songtan

    songtan

    Joined:
    Apr 9, 2011
    Posts:
    2,623
    Well, I didnt took it out intentionally. Just that there so many things when I was doing the upgrade that I missed a thing or two.

    To switch animation clip during runtime, you can refer to UnitAnimation.cs in the new version, specifically in Awake(). The code is to switch the default clip with the one specified by user. You can do the same thing after you randomly select a idle animation to play.
     
  47. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Cool ... thanks!
     
  48. Rajmahal

    Rajmahal

    Joined:
    Apr 20, 2011
    Posts:
    2,074
    Quick update ... I got the upgrade to Mechanim done in my version. Works really nicely and got it playing ability animations too. Very easy.

    Mechanim is seriously great ... blend trees and the transitions from one animation state to another are fantastic. Of course, I now have to go back and switch my earlier characters (all 112) to the mechanim versions. That will take a while. :(
     
  49. navy3000

    navy3000

    Joined:
    Nov 28, 2014
    Posts:
    106
  50. Pedro-Oliveira

    Pedro-Oliveira

    Joined:
    Aug 1, 2013
    Posts:
    25
    @songtan I want to change the language of some warnings like: "missed" or "can't use abilities". So I searched for a while but couldn't find then. Can you tell me where are they?
     
unityunity