Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jonathanbooker2911 · Dec 03, 2018 at 07:45 PM · scripting problemscript.scripting beginnerscriptingbasicsscript error

How do I make do I make a variable go back to its original value for a spell system

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class HumanRacial : MonoBehaviour
 {
     public float agility;
     public float strength;
     public float intellect;
     public float coolDown = 120;
     public float coolDownTimer;
     public float duration;
     public float durationTimer;
     public bool isWarrior;
     public bool isBountyHunter;
     public bool isMage;
 
     public void Update()
     {          
         Active();
         Cooldown();
         Wakeup();
     }
 
     public void RacialBonus()
     {
         if(isMage == true)
         {
             intellect += intellect;
         }
         if (isWarrior == true)
         {
             strength += strength;
         }
         if (isBountyHunter == true)
         {
             agility += agility;
         }
     }
 
     private void Cooldown()
     {
         if (duration > 0)
         {
             durationTimer -= Time.deltaTime;
         }
         if (durationTimer < 0)
         {
             durationTimer = 0;
         }
         if (coolDownTimer > 0)
         {
             coolDownTimer -= Time.deltaTime;
         }
         if (coolDownTimer < 0)
         {
             coolDownTimer = 0;
         }
     }
 
     void Wakeup()
     {
         if (durationTimer <= 0.1f && isWarrior == true)
         {
             strength = strength / 2f;
         }
         if (durationTimer <= 0 && isMage == true)
         {
             intellect = intellect / 2f;
         }
         if (durationTimer <= 0 && isBountyHunter == true)
         {
             agility = agility / 2f;
         }
     }
 
     private void Active()
     {
         if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
         {
             coolDownTimer = coolDown;
             durationTimer = duration;
             RacialBonus();
         }
     }
 }

How do I make the variable go back to normal after durationTimer hits zero.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Vega4Life · Dec 03, 2018 at 08:50 PM

This may be a good time to start using some scriptable objects. This allows you to store data separately for each racial type you have, and it also allows you to easily revert back to base data for each racial.

Here is some code for getting the idea across:


 using UnityEngine;
 
 
 public class HumanRacial : MonoBehaviour
 {
     // Racial is a scriptabl object, holding all your racial data for warrior, mage, bounty hunter
     [SerializeField] Racial racial;
 
     float currentAgility;
     float currentStrength;
     float currentIntellect;
 
     float coolDownTimer;
     float durationTimer;
 
 
     void Awake()
     {
         Setup();
     }
 
 
     void Setup()
     {
         currentAgility = racial.agility;
         currentIntellect = racial.intellect;
         currentStrength = racial.strength;
     }
 
 
     public void Update()
     {
         Active();
         Cooldown();
         Wakeup();
     }
 
 
     public void RacialBonus()
     {
         if (racial.racialType == Racial.RacialType.Mage)
         {
             currentIntellect += currentIntellect;
         }
         if (racial.racialType == Racial.RacialType.Warrior)
         {
             currentStrength += currentStrength;
         }
         if (racial.racialType == Racial.RacialType.BountyHunter)
         {
             currentAgility += currentAgility;
         }
     }
 
     private void Cooldown()
     {
         if (racial.duration > 0)
         {
             durationTimer -= Time.deltaTime;
         }
         if (durationTimer < 0)
         {
             durationTimer = 0;
         }
         if (coolDownTimer > 0)
         {
             coolDownTimer -= Time.deltaTime;
         }
         if (coolDownTimer < 0)
         {
             coolDownTimer = 0;
         }
     }
 
     void Wakeup()
     {
         if (durationTimer <= 0.1f && racial.racialType == Racial.RacialType.Warrior)
         {
             currentStrength = racial.strength;
         }
         if (durationTimer <= 0 && racial.racialType == Racial.RacialType.Mage)
         {
             currentIntellect = racial.intellect;
         }
         if (durationTimer <= 0 && racial.racialType == Racial.RacialType.BountyHunter)
         {
             currentAgility = racial.agility;
         }
     }
 
     private void Active()
     {
         if (Input.GetKeyDown(KeyCode.D) && coolDownTimer == 0 && durationTimer == 0)
         {
             coolDownTimer = racial.coolDown;
             durationTimer = racial.duration;
             RacialBonus();
         }
     }
 }



Here is the scriptable for your data:


 using UnityEngine;
 
 // Scriptable to hold all your racial data for each class
 // Each class can have its seperate dat
 // Create a racial by going to assets -> create -> HumanRacial
 [CreateAssetMenu(menuName = "HumanRacial/Racial")]
 public class Racial : ScriptableObject
 {
     public enum RacialType { Warrior, Mage, BountyHunter };
 
     public RacialType racialType;
     public float agility;
     public float strength;
     public float intellect;
     public float coolDown;
     public float duration;
 }



This type of system makes it easy to interchange racials, get the correct data, etc. Nothing touches the racials base data, so its easy to reset.


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jonathanbooker2911 · Dec 04, 2018 at 01:51 AM 0
Share

I have new problem now floats post numbers like 1.50008 but I need it to hit 1f for the stat change. Any idea of a way around that.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

180 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

3rd Person Camera to Rotate With the Player 0 Answers

How do I make my projectile deal damage to a target it hit. 1 Answer

Need help fixing my script 1 Answer

How to share varibles betweent scripts 4 Answers

how can I change a light with multiple triggers. ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges