Search Unity

Unity 5: How do I make companions/pets have an affection level with Main Character?

Discussion in 'Scripting' started by Ardiohsis, Nov 24, 2017.

  1. Ardiohsis

    Ardiohsis

    Joined:
    Nov 22, 2017
    Posts:
    7
    Hi, I am fairly new to Unity. I have searched everywhere and have not found an answer, how do I make companions have an affection level with the main character? An example would be finding a piece of meat, feeding it to the companion, now he likes me better and obeys better orders. Another example would be I haven't fed the companion in 3 hours game time, now the companion doesn't like me as much. Are there any assets to this? If it is a scripted answer, please, please, please explain in detail, as I am new to scripting, although I understand how it works (A video guide would be the bomb!). Thank you!!!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I have absolutely no idea if there's an asset for that (maybe). You could try looking in the store, or waiting to see if someone else has a suggestion.
    ** I have to add, I think it's a somewhat bad idea to jump to an asset, though, if you are so new and don't program at all.. I doubt you'd get far with that asset alone. Just being honest.
    Even if it were in the store, of course it would be a scripting solution ;)

    If you are new, you should try the building blocks of Unity. Get familiar with the basics (especially things you wnat to use in Unity), as well as scripting introductions and/or some simple tutorial lessons and/or games :)
    That's my best advice. Even the little things you learn there will be useful in other places.
     
  3. Ardiohsis

    Ardiohsis

    Joined:
    Nov 22, 2017
    Posts:
    7
    Thank you for the advice and I hear you. If there was a video or picture instructions to guide me I would easily be able to script it. This is my first time using Unity and something as serious as Unity, but I do have experience in game creating. Warcraft 3's Map Editor helped me learn a lot back when the game was popular. Did some coding/scripting on it's platform. Although, if nobody can provide a video or images I'm screwed, at least until I figure it out myself. ):
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    This isn't just some simple request, unfortunately. While I could work out the logic in my head and program something in, it's a lot more then one could easily just share.

    The basic logic though is you have items you can pick up with some option to feed to your pet. Once fed, you increase some affection value and start/restart a timer that counts down to track when affection should go down if not fed or interacted with or whatever to keep affection even or increase it. Granted you might set up, if they feed the pet, the timer is longer. If they pet the pet, maybe it's shorter. But you also need some check probably to not reset the timer if say, you get 3 hours for feeding, but then you pet and normally only get 1 hour for petting for example.

    Then of course, you need to track the timer with a save so you can restart it at where it left off.

    This is all just thinking it through, but I'm afraid I have no examples nor would I have the time to write all this up for you.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Like he said, think about the "parts" you need and work on those. That's related to why I mentioned the introduction/learning steps. Because often those are parts, you could/would use here or what not ;)

    Also doesn't hurt to try a smaller goal. Can I get an action -> reward. Can I get a timer to keep affection up/lower it. Stuff like that. If you can get one or more of those, ... (so on) :)
     
  6. Ardiohsis

    Ardiohsis

    Joined:
    Nov 22, 2017
    Posts:
    7
    Thank you guys very much, I have a much better idea on how it would work. You guys made this idea possible for me. Will most likely take a few weeks or so to get it down. If anyone can provide images or videos it would most likely speed up my process and make more sense via Unity. I would be willing to give a donation. Not to mention, you would be on a post that has great answers, without step by step guides.
     
  7. Kaart

    Kaart

    Joined:
    Jul 31, 2017
    Posts:
    62
    I was bored so I made a small example of how you could implement a system like this. Use the code below and use your affection when performing certain actions with your pet. For example you can make an attack random hit or mis based on the affection you have. Hope this helps.

    Code (CSharp):
    1. public class PetExample : MonoBehaviour {
    2.     public int affection = 0;
    3.     public int maxAffection = 100;
    4.     public int minAffection = 0;
    5.  
    6.     // Time in seconds it would take to reduce your pet's affection after it has been feed/petted.
    7.     public float affectionDecayTick = 60;
    8.  
    9.     // Amount of affection lost after each decay tick
    10.     public int affectionDecayAmount = 3;
    11.  
    12.     float affectionDecayStartTime;
    13.  
    14.     // Use this for initialization
    15.     void Start () {
    16.         affectionDecayStartTime = Time.time;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         // Time past after we have last feeded / petted our pet.
    22.         float timePast = Time.time - affectionDecayStartTime;
    23.        
    24.         // If more time has past than our affectionDecayTick we reduce our affection.
    25.         if (timePast >= affectionDecayTick)
    26.         {
    27.             affection -= affectionDecayAmount;
    28.            
    29.             // Limits your affection to a certain minimum
    30.             if (affection < minAffection)
    31.             {
    32.                 affection = minAffection;
    33.             }
    34.  
    35.             // Reset the affection decay timer.
    36.             affectionDecayStartTime = Time.time;
    37.         }
    38.     }
    39.  
    40.     // Adds an amount of affection to your total.
    41.     private void AddAffection(int amountOfAffection)
    42.     {
    43.         affection += amountOfAffection;
    44.  
    45.         // Limits your affections to a certain maximum
    46.         if (affection > maxAffection)
    47.         {
    48.             affection = maxAffection;
    49.         }
    50.  
    51.         // Reset the affection decay timer.
    52.         affectionDecayStartTime = Time.time;
    53.     }
    54.  
    55.     // Feed your pet, call this method from your player
    56.     public void Feed()
    57.     {
    58.         // Play your feeding animation.
    59.         AddAffection(3);
    60.     }
    61.  
    62.     // Pet your pet, call this method from your player
    63.     public void Pet()
    64.     {
    65.         // Play your petting animation.
    66.         AddAffection(1);
    67.     }
    68. }
    69.  
     
  8. Ardiohsis

    Ardiohsis

    Joined:
    Nov 22, 2017
    Posts:
    7
    Thank you so much! This should be a great help. Will start working on it tomorrow. Thank you to everyone that gave advice and helped me. Hopefully this project comes out as good as I imagine it to be. :)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Good luck with your project :)
     
    Ardiohsis likes this.
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,702
    FWIW, there is an asset exactly for this: Love/Hate.

    But it was super awesome for @Kaart to write some nice starter code. Good luck on your project, @Ardiohsis!