Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[RELEASED] Turn Based Strategy Framework

Discussion in 'Assets and Asset Store' started by michal-zetkowski, Jul 2, 2019.

  1. asaarashi

    asaarashi

    Joined:
    Jan 19, 2021
    Posts:
    3
    Hi, I launched my project with this excellent asset.

    I want to achieve that the cells around an opponent unit of a unit make the unit's movement cost +1. But I found
    Code (CSharp):
    1. GetAvailableDestinations
    cannot be override, how can I do that?

    Thanks!
     
  2. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Hey, I intended GetAvailableDestinations to be kind of a helper method that should not be virtual. You are free to change that, or modify the method directly :). I think it should be enoughto achieve effect that you want.

    A cleaner solution would be to add GetMovementCost() method to Cell. you would need to replace all calls to cell.MovementCost with cell.GetMovementCost ofc. In your class derieved from cell, you could define that cells that have neighbour with an enemy unit have +1 movement cost.
     
  3. asaarashi

    asaarashi

    Joined:
    Jan 19, 2021
    Posts:
    3
    I can do that but I hope reading `Cell.MovementCost` could be replaced by `Cell.GetMovementCost()` in the asset release because I think to customize movement cost in a various way is common in customization. I don't want to modify the asset native code directly because that would bring trouble when upgrading the asset in the future. Thanks for the quick reply!
     
    Last edited: Feb 1, 2021
  4. asaarashi

    asaarashi

    Joined:
    Jan 19, 2021
    Posts:
    3
    I am experiencing this as well, haha..Hope you could fix it
     
  5. LordDooms

    LordDooms

    Joined:
    Sep 20, 2014
    Posts:
    8
    Hey when can we see a demo? Looks very cool.
     
    Last edited: Feb 5, 2021
    michal-zetkowski likes this.
  6. LordDooms

    LordDooms

    Joined:
    Sep 20, 2014
    Posts:
    8
    Loving the tool kit. Quick coding approach question. If you have buildings like a city, mine, village, etc that can change ownership by attacking or walking on (if empty), would you make them part of the map tile or a new unit type? Does it make a difference if you can raze them?
    screenshot_week2.png
     
  7. blackant

    blackant

    Joined:
    Jun 18, 2009
    Posts:
    529
    Hello,
    i got a bug on your framework.
    This happens when you double clic- triple-clic or quadruple-clic... on a enemy sprite.
    after,it locks completely the gameplay, unable to clic anywhere, (button or other sprites)
     
  8. Untrustedlife

    Untrustedlife

    Joined:
    Apr 21, 2020
    Posts:
    78
    I made mine into units, and you can "Ruin" towns in my game aswell so. It requires some code for changing the player number on the town and such but its pretty painless.
     
    LordDooms likes this.
  9. Untrustedlife

    Untrustedlife

    Joined:
    Apr 21, 2020
    Posts:
    78
    Hey there Michal!
    I noticed in the newer version of your framework you say added compatibility to unity 2020, however i do not wish to update to the newest version because i made significant changes to the code your asset provides already. I updated to unity 2020 on a copy of the project and it worked just fine, so what exactly did you have to fix for compatibility? as far as i can tell there are no issues with your frameworks compatibility to unity 2020 at all in the previous version. If you can give me a list so i can fix it locally, if it is say, something that doesn't come up much that would help me significantly. Or maybe i just so happened to change those things on my end already and didnt have to worry about it.
     
  10. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    @DoomVegan Thanks, good to hear that you like it :). Take a look at the previous posts where we discussed using Units as cities with @Untrustedlife. I still think this is the way to go. Also, I'm working on a new release that kind of as a side effect facilitates this approach.

    In coming weeks I'll post here about the upcoming changes, I'm curious about your opinions.

    @blackant Hmm, thanks for letting me know, I'll check it out. Does it happen in example scenes or your own?

    @Untrustedlife As far as I remember, this entry in changelog meant that I verified that it works with Unity 2020 and uploaded the package using this version :) So nothing to worry about.
     
    Untrustedlife likes this.
  11. blackant

    blackant

    Joined:
    Jun 18, 2009
    Posts:
    529
    it happens everywhere, but maybe i realize, by clicking faster than the animation speed, that may be all the action points that are used...
     
  12. Dashzed

    Dashzed

    Joined:
    May 1, 2019
    Posts:
    6
    Hi Michael! First off, I just wanted to say this is a fantastic product and I would have paid 5x the price for it(even more if a few more unit features were built in. Maybe consider a V2 in the future?).

    I'm working on making a single player zombie game, and I was wondering if you had any insight on how I could do two things:

    1. When a Zombie unit attacks a player unit of a certain type(let's say non-vehicle for simplicity), I want it to apply essentially a poison effect that ticks down the unit's health each turn until they die.

    2. When a player unit either dies from the afore-mentioned "poison" effect, or if a Zombie unit straight up kills them, I want to spawn a fresh zombie unit in its place.

    If you or anyone else has any ideas for how I could implement either or both of these I would be forever grateful! My whole game kinda hinges on making both of those happen.
     
  13. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Glad you like it :). Actually I'm working on a new version, some pretty fun features are coming. Not gonna increase the price though.

    Regarding poison effect, it sounds like a good use for built-in buff system. Take a look at HealingBuff from Example1. You would need something similar, just pass negative healing factor.

    Next issue is how to apply this buff on attack. You will probably need to make Unit.AttackHander method virtual and override it in your Zombie class. You could apply the poison there.

    Finally, you will need to know if the unit was killed by poison (that would cause transformation) or normal attack. I would make the buff call some intermediate method on your other unit where you would apply the damage and then check the HP. Based on this, you can spawn new unit.

    Not sure if you know how to spawn units on runtime - basically instantiate the prefab normally, initialize spawned unit Cell field and position and finally call CellGrid.AddUnit with the new unit as parameter. You can find the details somewhere in this thread, it was mentioned a few times.

    Another approach could be transforming the unit without spawning new object. Not sure how viable it is.

    Let me know if you have more questions :)
     
  14. Dashzed

    Dashzed

    Joined:
    May 1, 2019
    Posts:
    6
    Thanks so much Michael! Here is what I tried(I know it's definitely not correct):

    public GameObject unitToCreate; //I added the Zombie unit that I wanted to spawn to this

    protected override void OnDestroyed()
    {
    Instantiate(unitToCreate, transform.position, transform.rotation);
    unitToCreate.transform.position = sourceCell.transform.position;
    cellgrid.AddUnit(unitToCreate.GetComponent<Transform>());
    Destroy(gameObject);

    }

    I'm getting the following errors:
    Assets\Functioning Prototype 1\Scripts\Alien.cs(46,58): error CS1061: 'object' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

    Assets\Functioning Prototype 1\Scripts\Alien.cs(47,13): error CS0103: The name 'cellgrid' does not exist in the current context
     
  15. CarsteinLeung

    CarsteinLeung

    Joined:
    Jan 9, 2017
    Posts:
    5
    Hi Michael, I am new to unity and try to use your asset to build my turn base game. It is very good and helpful. Thank you very much for create this tools!

    I am now using some 3d models to replace the hero/paladin in the example one. However, I found that the models don't know how to rotate to face their moving directions. When I try to modify the code in class "Unit.Move", the models will face incorrect direction or up-side-down. Would you please tell me if I did anything wrong or you already have function for it?

    Thank you very much and look for your reply.
     
  16. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    It's almost good, try this
    Code (CSharp):
    1. public GameObject unitToCreate; //I added the Zombie unit that I wanted to spawn to this
    2.  
    3.         protected override void OnDestroyed()
    4.         {
    5.             var unitGO = Instantiate(unitToCreate, transform.position, transform.rotation);
    6.             unitGO.transform.position = sourceCell.transform.position;
    7.             unitGO.GetComponent<Unit>().Cell = someCell // you need reference to cell where the unit will be spawned, probably deadUnit.Cell
    8.             FindObjectOfType<CellGrid>().AddUnit(unitGO.GetComponent<Transform>());
    9.             Destroy(gameObject);
    10.  
    11.         }
     
    Last edited: Mar 9, 2021
  17. CarsteinLeung

    CarsteinLeung

    Joined:
    Jan 9, 2017
    Posts:
    5
    Hi Banksy, I am sorry that I am new to Unity. I have the same problem as what you have. What I can do is to find out that the model have some facing problem. If you use the normal way to rotate the gameobject it does not work.
    I tried to just rotate the object in Z-axis

    protected virtual IEnumerator MovementAnimation(List<Cell> path)
    {
    IsMoving = true;
    path.Reverse();
    foreach (var cell in path)
    {
    Vector3 destination_pos = new Vector3(cell.transform.localPosition.x, cell.transform.localPosition.y, transform.localPosition.z);

    while (transform.localPosition != destination_pos)
    {
    transform.localPosition = Vector3.MoveTowards(transform.localPosition, destination_pos, Time.deltaTime * MovementAnimationSpeed);

    transform.Rotate(0,0, 2.940f,Space.Self);
    //Debug.Log(transform.position);

    yield return 0;
    }
    }
    IsMoving = false;
    OnMoveFinished();
    }

    And need to determine how many degrees you need to rotate(I am using HexGrid so every movement so be 60 degrees*X)

    I don't know if this can help or not.....
     
  18. DarkXPhoton

    DarkXPhoton

    Joined:
    Feb 3, 2021
    Posts:
    6
    I tried to follow the documentation but it's not working. I'm not able to see the grid (the CellGrid, Players, etc. are created but nothing happens to the scene both in scene and game mode). I tried both with the tile HexagonPrefab and with the cube from the tutorial and neither is working. I also tried tile painting and unit painting and nothing happens.

    Also when I follow the tutorial and use Square, there is an error. Do I need a using statement to use Square, if yes what is it?

    upload_2021-2-19_16-10-48.png upload_2021-2-19_16-10-48.png

    I just bought and downloaded TBS framework so I should have the latest version. My version of unity is 2019.4.20f1. I have a windows 10.
     
    Last edited: Feb 20, 2021
  19. Dashzed

    Dashzed

    Joined:
    May 1, 2019
    Posts:
    6
    Thank you Michael!

    I'm getting a few errors still, on the sourceCell part of:
    unitGO.transform.position = sourceCell.transform.position;

    Assets\Functioning Prototype 1\Scripts\Alien.cs(57,52): error CS1061: 'object' does not contain a definition for 'transform' and no accessible extension method 'transform' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

    And on the deadUnit.Cell part

    Assets\Functioning Prototype 1\Scripts\Alien.cs(58,48): error CS0103: The name 'deadUnit' does not exist in the current context
     
  20. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    @CarsteinLeung @Banksy This is a known issue, it's my fault because I did not use Unity default rotation for prefabs. Recently I came up with something like this, try it out

    Code (CSharp):
    1.  
    2. protected virtual IEnumerator MovementAnimation(List < Cell > path)
    3. {
    4.     IsMoving = true;
    5.     path.Reverse();
    6.     foreach(var cell in path) {
    7.         float angleRad = Mathf.Atan2(cell.transform.localPosition.x - transform.localPosition.x, cell.transform.localPosition.y - transform.localPosition.y);
    8.         float angleDeg = (180 / Mathf.PI) * angleRad * -1;
    9.         transform.localRotation = Quaternion.Euler(0, 0, angleDeg);
    10.         Vector3 destination_pos = new Vector3(cell.transform.localPosition.x, cell.transform.localPosition.y, transform.localPosition.z);
    11.         while (transform.localPosition != destination_pos) {
    12.             transform.localPosition = Vector3.MoveTowards(transform.localPosition, destination_pos, Time.deltaTime * MovementAnimationSpeed);
    13.             yield
    14.             return 0;
    15.         }
    16.     }
    17.     IsMoving = false;
    18.     OnMoveFinished();
    19. }
    20.  
     
    CarsteinLeung likes this.
  21. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Can you show me your Tile prefab? You're getting an error saying that you provided wrong Hexagon prefab, probably it is set up incorrectly
     
  22. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Regarding deadUnit, it was pseudocode, I meant that you need to extract the cell from the unit that died :) If OnDestroyed method is on the unit itself, then it will be just unitGO.GetComponent<Unit>().Cell = this.Cell

    About the other error, that's strange, guess I'm missing something. How about you comment out that line? You are setting up the position in Instantiate anyway.
     
  23. CarsteinLeung

    CarsteinLeung

    Joined:
    Jan 9, 2017
    Posts:
    5

    This seems fixed my problem!!!! Thank you very much Michael!
     
  24. DarkXPhoton

    DarkXPhoton

    Joined:
    Feb 3, 2021
    Posts:
    6
    I tried the tile prefab that is part of the package.
    upload_2021-2-20_14-5-47.png

    I also tried a few other (RoadTile from the package, Cube as said in the tutorial, etc.) None of them work.
     
  25. DarkXPhoton

    DarkXPhoton

    Joined:
    Feb 3, 2021
    Posts:
    6
    I have those errors

    upload_2021-2-20_14-13-57.png
     
  26. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    @DarkXPhoton It seems that you are trying to use square prefab with hex grid generator. Either provide hexagon prefab, or select RectangularSquareGridGenerator in GridHelper
     
  27. Jor_Brando

    Jor_Brando

    Joined:
    Jan 24, 2021
    Posts:
    4
    Hello, sir! Great asset! I was wondering if there is a way to make it so that when your mouse hovers over a unit, you can show an individualized portrait that is specific to that particular unit. As of now, there is a general unit panel but it isn't unit specific when you hover; it just shows you their stats.
     
  28. Dashzed

    Dashzed

    Joined:
    May 1, 2019
    Posts:
    6
    I wanted to share my solutions for the zombie spawning, and the bitten/damage over time functions. I am having an issue when a Human unit dies of the zombie damage over time though, it triggers this error:

    MissingReferenceException: The object of type 'Human' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    TbsFramework.Example2.Zombie+<Jerk>d__8.MoveNext () (at Assets/Dead Command/Scripts/Units/Zombie.cs:64)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <31d5d65b32ec483292e13e8ae4100b93>:0)
    UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
    TbsFramework.Example2.Zombie:MarkAsAttacking(Unit) (at Assets/Dead Command/Scripts/Units/Zombie.cs:52)
    TbsFramework.Example2.Zombie:AttackHandler(Unit) (at Assets/Dead Command/Scripts/Units/Zombie.cs:36)
    TbsFramework.Players.<Play>d__4:MoveNext() (at Assets/TBS Framework/Scripts/Players/NaiveAiPlayer.cs:52)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    Anyhow, here's my current(working except for the previous error) code. I added "Infected" as a public bool on my Unit class, and added this to the Zombie Unit:

    public override void AttackHandler(Unit unitToAttack)
    {
    if (!IsUnitAttackable(unitToAttack, Cell))
    {
    return;
    }

    else
    {
    AttackAction attackAction = DealDamage(unitToAttack);
    MarkAsAttacking(unitToAttack);
    unitToAttack.DefendHandler(this, attackAction.Damage);
    AttackActionPerformed(attackAction.ActionCost);
    unitToAttack.Infected = true;
    }
    }

    Then I added this to the Human Unit:

    Coroutine PulseCoroutine;
    private BuffSpawner _buffSpawner;

    public override void OnTurnEnd()
    {
    if (Infected != true)
    {
    //return;
    base.OnTurnEnd();
    }
    else
    {
    _buffSpawner = new BuffSpawner();
    _buffSpawner.SpawnBuff(new BittenBuff(1, 2), Cell, this, 0, true);
    base.OnTurnEnd();
    }
    }

    protected override void OnDestroyed()
    {
    //unitToCreate is a public GameObject variable. it represents the prefab to create

    var cellgrid = FindObjectOfType<CellGrid>();
    if (unitToCreate == null)
    {
    return;
    }
    GameObject createdUnit = Instantiate(unitToCreate);
    Cell.IsTaken = true;
    createdUnit.transform.position = Cell.transform.position;
    createdUnit.GetComponent<Unit>().Cell = Cell;
    Cell.CurrentUnit = createdUnit.GetComponent<Unit>();
    createdUnit.GetComponent<Unit>().PlayerNumber = 1;
    createdUnit.GetComponent<Unit>().Initialize();
    createdUnit.transform.SetParent(cellgrid.GetComponent<CustomUnitGenerator>().UnitsParent);
    cellgrid.AddUnit(createdUnit.GetComponent<Transform>());
    Cell.IsTaken = false;
    MarkAsDestroyed();
    Destroy(gameObject);
    }

    Hope this helps someone, even if it's a bit messy! Also Michael, any idea why I'm getting that Missing Reference Exception? Has this been a problem for anyone else? Thanks!
     
  29. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    In Example1, in stats panel the color of player that the unit belongs to is displayed. You could do it in similar way. Add image field to your unit class, assign the image to prefabs, and the rest you can figure out by following Example1 - check out GuiController.OnUnitHighlighted
     
  30. Jor_Brando

    Jor_Brando

    Joined:
    Jan 24, 2021
    Posts:
    4
    Excellent! Thank you so much, sir. Much appreciated. Keep up the great work!
     
  31. DarkXPhoton

    DarkXPhoton

    Joined:
    Feb 3, 2021
    Posts:
    6
    I followed the tutorial and this is my scene.
    upload_2021-2-24_16-9-19.png
    The rocks and units are not exactly at the same place but it shouldn't affect how it works.

    However, when I click on play I get that
    upload_2021-2-24_16-10-4.png

    It's rotated, the colors are not the same and it is too zoomed in to see all the pieces.

    Also, when I click on a unit, the bottom left one changes to green but not the top one. Also, it is impossible to move the units. If I click on a unit, no matter which square I click afterward, it doesn't move. I can't slide the unit either.

    upload_2021-2-24_16-13-54.png

    How can I fix that?
     

    Attached Files:

  32. Zuljah

    Zuljah

    Joined:
    Jan 17, 2021
    Posts:
    3
    Hey all!
    Thanks for the asset, i´m using it and it seems to do what i want so far :). I´m currently working on spawning units on runtime. Now I have a small problem where when I go into unit placement mode, I want to cancel any movement that might be planned. So if the player clicks on a unit to move it, then decides otherwise and clicks on the button to place a unit, how would I deselect the first unit and remove the movement highlights and so on?
     
  33. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    To "deselect" a unit, simply assign new CellGridStateWaitingForInput object to CellGrid.GridState :)

    @DarkXPhoton Can you show me what parameters you assigned to your units?
     
  34. DarkXPhoton

    DarkXPhoton

    Joined:
    Feb 3, 2021
    Posts:
    6
    I put this in the unit inspector
    upload_2021-2-26_15-24-38.png
    upload_2021-2-26_15-25-8.png
    upload_2021-2-26_15-25-56.png
    for the blue units. For the red units, it's the same but player number 0.

    I put this in the code.
    using UnityEngine;
    using TbsFramework.Units;
    public class TestUnit : Unit
    {
    public Color LeadingColor;

    public override void Initialize()
    {
    base.Initialize();
    transform.localPosition -= new Vector3(0, 0, 1);
    GetComponent<Renderer>().material.color = LeadingColor;
    }
    public override void MarkAsFriendly()
    {
    GetComponent<Renderer>().material.color = LeadingColor + new Color(0.8f, 1, 0.8f);
    }
    public override void MarkAsReachableEnemy()
    {
    GetComponent<Renderer>().material.color = LeadingColor + Color.red;
    }
    public override void MarkAsSelected()
    {
    GetComponent<Renderer>().material.color = LeadingColor + Color.green;
    }
    public override void UnMark()
    {
    GetComponent<Renderer>().material.color = LeadingColor;
    }
    public override void MarkAsAttacking(Unit other)
    {
    }
    public override void MarkAsDefending(Unit other)
    {
    }
    public override void MarkAsDestroyed()
    {
    }
    public override void MarkAsFinished()
    {
    }
    }​
     
  35. DarkXPhoton

    DarkXPhoton

    Joined:
    Feb 3, 2021
    Posts:
    6
    Now I can actually move the one unit on the bottom left corner once. But I still don't know:
    -How to see everything? Rignt now it is zoomed too much and I can see only one of the 3 units.
    -How to see things in the same color and orientation in the scene and game?
    -How to end a turn so the 2nd player can play?
     
  36. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    The first two questions are not related to the framework, but general Unity questions. You need to adjust the camera and scene view manually. You end the turn by pressing 'n' on the keyboard
     
  37. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    Hi, I recently moved over to your framework and have been enjoying building up the various features like fog of war, unit sight and bases etc etc.

    However I have hit a bit of a brick wall regarding saving the game. I've turned to a 3rd party solution which is reasonable as I've played around with it before, however to get units to save/load properly I need to instantiate them at run time.

    So for this I thought of a unit spawner that can be placed via the grid helper to spawn the unit at runtime. But that is just not going to work it seems as it's a) not a unit and b) I can't get my head around how AddUnit actually works.

    Looking for some advice please :)

    EDIT: I would rather not use a 3rd party system for save/loading but have yet to suss an effective solution myself.
     
  38. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Hey,

    Spawning units on runtime goes like this:

    Code (CSharp):
    1. var unitGO = Instantiate(unitToCreate, transform.position, transform.rotation);
    2. unitGO.transform.position = sourceCell.transform.position;
    3. unitGO.GetComponent<Unit>().Cell = someCell // you need reference to cell where the unit will be spawned
    4. FindObjectOfType<CellGrid().AddUnit(unitGO.GetComponent<Transform>());
    Basically, instantiate the prefab, set up the position and Cell, and pass the transform component to CellGrid.AddUnit. In your case you will also need to fill in all the attributes from the serialized unit (HP, MovementPoints etc). Let me know if you have more questions :)
     
    Last edited: Mar 9, 2021
  39. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    Thank you, I do a have a few more bits to ask.

    1. The above code, I assuming you add that as a method to the individual unit's script so you could work it with a button for purchasing the unit for example. And then you can place it on the map.. which provides the someCell.

    2. Although I'm a bit lost with the rest of it, any chance of a bit more in-depth tutorial?
     
  40. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    I've been working on this a fair bit today but not joy. While I've found most of the asset pretty easy to understand and modify, the cell stuff seems some what confusing and not very accessible.

    I looked at the code you provided and started to think how to implement it, the bit I decided to tackle first was getting a cell reference.

    So, I duplicated and modify the movement range code in units.cs (I have done this for sight range and it works nicely) so I created a new method Buildrange(); Which is fine, but I'm still not sure how to continue.

    The idea behind it was for the button to activate a grid around a base unit to then I could get a cell reference for addunit.

    Here is a picture of my game.

    upload_2021-3-6_22-41-16.png
     
    Last edited: Mar 6, 2021
  41. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Hey,

    Did you try getting the reference to the cell from CellGridStateUnitSelected class? You can grab it in OnCellClicked method. You will need to add some condition to distinguish between moving and spawning state, which will not be very pretty, but it will work.

    Let me know if that helped
     
  42. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    No, not so far. I can't access the OnCellClicked method from outside of the CellGridStateUnitSelected script.

    I see what you mean about the conditions, so I did a isbuilding bool and an if statement to cover that, but I still don't understand how to access it from anywhere else.

    I seem to be going round in circles a bit with this, I'm not even sure where I would put that original code to instantiate the unit now.
     
    Last edited: Mar 7, 2021
  43. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    Yeah, but how about the other way around? Instead of accessing OnCellClicked from Unit, access some method on Unit from OnCellClicked?
     
  44. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    So.. Have the spawn code in the actual unit script and then send the cell ref to the unit script?
     
  45. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    That's how I would do it :)
     
  46. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    Ok, So I've set it up so that I can click build mode, a grid pops up round my base and any hex I select within the build range can do something, this is all in CellGridStateUnitSelected.

    I still can't seem to link it to actually adding a unit, I can't access methods I make in the unit scripts, unless its _unit for the selected unit.

    EDIT:

    After working on this a bit more, I've made a new method inside CellGrid that returns a cell from CellGridStateUnitSelected and spawns a unit. I've got them todo unit.Initialize but can't select them atm :p.
     
    Last edited: Mar 7, 2021
  47. Dashzed

    Dashzed

    Joined:
    May 1, 2019
    Posts:
    6
    Hi Michael, do you know if there’s any way to have 3d units on a 2d grid? Essentially I just want to use sprites for my hexes, but use 3d for everything else. I spent quite a while failing to implement that today, but I’m having a hard time getting it all to work properly. Thanks!
     
  48. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    @Dashzed I've just checked on my project which is all 2d, but you can add models to unit prefabs and then get the units to face the correct direction when moving by getting the positions from destination_pos in MovementAnimation from unit.cs.
    Also works for attacking as well.


    Code (CSharp):
    1. protected virtual IEnumerator MovementAnimation(List<Cell> path)
    2.         {
    3.             IsMoving = true;
    4.             UnitFacingMove = transform.localPosition.x;
    5.             path.Reverse();
    6.  
    7.             foreach (var cell in path)
    8.             {
    9.                 Vector3 destination_pos = new Vector3(cell.transform.localPosition.x, cell.transform.localPosition.y, transform.localPosition.z);
    10.                 UnitFacing = destination_pos.x;
    11.  
    12.                 while (transform.localPosition != destination_pos)
    13.                 {
    14.                
    15.                     transform.localPosition = Vector3.MoveTowards(transform.localPosition, destination_pos, Time.deltaTime * MovementAnimationSpeed);
    16.                     yield return 0;
    17.                     UnitFacing = destination_pos.x;
    18.                                        
    19.                     if (UnitFacingMove < UnitFacing)
    20.                     {
    21.                         UnitSprite.flipX = false;
    22.                         Debug.Log("right");
    23.                     }
    24.                     if (UnitFacingMove > UnitFacing)
    25.                     {
    26.                         UnitSprite.flipX = true;
    27.                         Debug.Log("left");
    28.                     }
    29.                 }
    30.  
    31.             }
    My game only needs the unit sprite to flip left or right but I'm pretty sure you can modify this to set the unit rotation as well.

    Unity - Scripting API: Vector3.RotateTowards (unity3d.com)
    This might help as well.
     
    Last edited: Mar 8, 2021
  49. Liberation85

    Liberation85

    Joined:
    Apr 16, 2019
    Posts:
    65
    @michal-zetkowski Ok, so this is my code atm

    Code (CSharp):
    1.    public void AddInf(Cell cell)
    2.         {
    3.  
    4.             CellGrid _cellGrid = FindObjectOfType<CellGrid>(); // just so I can use find object once.
    5.             Unit unitToCreate = TIinF; // Public gameobejct
    6.             Cell sourceCell = cell; // Cell clicked on
    7.  
    8.             var unitGO = Instantiate(unitToCreate, transform.position, transform.rotation);
    9.             unitGO.transform.position = sourceCell.transform.position;
    10.             sourceCell.IsTaken = true;
    11.             sourceCell.CurrentUnit = unitGO; // assign the unit to this cell
    12.             sourceCell.UnitHere = unitGO; //fog of war
    13.            // unitGO.Cell = sourceCell;
    14.             unitGO.PlayerNumber = 0;
    15.             unitGO.Initialize();
    16.             unitGO.transform.SetParent(_cellGrid.GetComponent<CustomUnitGenerator>().UnitsParent); // for neatness
    17.             _cellGrid.AddUnit(unitToCreate.GetComponent<Transform>());
    18.         }
    It spawns the unit in the correct place, initalizes it, so the health etc etc seems correct.

    But I can't select the unit and when I select another unit and move the mouse over a moveable hex I get this warning..

    upload_2021-3-8_11-16-8.png

    ( int distance = (int)(Mathf.Abs(CubeCoord.x - _other.CubeCoord.x) + Mathf.Abs(CubeCoord.y - _other.CubeCoord.y) + Mathf.Abs(CubeCoord.z - _other.CubeCoord.z)) / 2;)

    And if I try to end the turn I get this...

    upload_2021-3-8_11-16-56.png

    (UnitState.MakeTransition(state);)

    Ignore the emotes...
     
  50. michal-zetkowski

    michal-zetkowski

    Joined:
    Mar 11, 2015
    Posts:
    282
    @Dashzed What issues are you having exactly? I don't see any reason why it wouldn't work, one thing to note is that 2D colliders work only in XY plane, so either make sure your scene is rotated correctly or use 3D colliders for the spires.

    @Liberation85 Would it be possible to send me exported project by email? :)