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 /
  • Help Room /
avatar image
0
Question by ChrisAnderson · Jan 23, 2017 at 03:47 PM · scripting problemanimator

Needs script trigger animation?

Hello,

I'm building a pet simulator based off of the Chao Garden from the Sonic games, and right now I'm working on scripting a needs system that will trigger an animation on the pet once the need is empty. This is what I have so far:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class ChaoNeeds : MonoBehaviour { public Animator anim; public Slider happinessSlider; public int maxHappiness; public int hapFallRate;

 public Slider sleepSlider;
 public int maxSleep;
 public int slpFallRate;

 public Slider hungerSlider;
 public int maxHunger;
 public int hgrFallRate;

 private float haplow;
 private float slplow;
 private float hgrlow;
 private bool run;

 void Start()
 {
     anim = GetComponent <Animator>();
     happinessSlider.maxValue = maxHappiness;
     happinessSlider.value = maxHappiness;

     sleepSlider.maxValue = maxSleep;
     sleepSlider.value = maxSleep;

     hungerSlider.maxValue = maxHunger;
     hungerSlider.value = maxHunger;
     run = false;
 }

 void Update()
 {
     //HAPPINESS CONTROLLER
     if(hungerSlider.value <= 0 && (sleepSlider.value <= 0))
     {
         happinessSlider.value -= Time.deltaTime / hapFallRate * 2;
     }

     else if(hungerSlider.value <= 5 || sleepSlider.value <= 5)
     {
         happinessSlider.value -= Time.deltaTime / hapFallRate * 2;
     }

     if(happinessSlider.value <= 0)
     {
         anim.Play("Crying");
     }

     //HUNGER CONTROLLER
     if (hungerSlider.value >= 0)
     {
         hungerSlider.value -= Time.deltaTime / hgrFallRate;
     }

     else if (hungerSlider.value <= 0)
     {
         hungerSlider.value = 0;
         anim.Play("Crying");
     }

     else if (hungerSlider.value >= maxHunger)
     {
         hungerSlider.value = maxHunger;
     }

     //SLEEP CONTROLLER
     if (sleepSlider.value >= 0)
     {
         sleepSlider.value -= Time.deltaTime / slpFallRate;
     }

     else if (sleepSlider.value <= 0)
     {
         sleepSlider.value = 0;
     }

     else if (sleepSlider.value >= maxSleep)
     {
         sleepSlider.value = maxSleep;
     }
 }

 void ChaoFoodSearch ()
 {
     //CHAO WILL BEGIN TO EAT FRUIT ONCE WITHIN IT'S RANGE
 }

}

The needs system is working and the pet needs are degrading as they should be. Where I've hit a wall is getting the animations to play once the need reaches a certain point. Part of the reason is because I'm not entirely sure how to set up my animator for something like this. This is the animator currently:

alt text

If anyone thinks they can be of assistance it would really help me out a lot.

animscreenshot.png (32.7 kB)
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
0
Best Answer

Answer by UnityCoach · Jan 23, 2017 at 09:52 PM

First, you need to add Animation parameters to your Animator Controller, and set those parameters as conditions for your transitions. Then, through code, you simply use SetTrigger to trigger your transitions. Note, you can also use Boolean, or Float, or Int parameters.

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 ChrisAnderson · Jan 25, 2017 at 12:13 AM 0
Share

Thanks. I actually found a tutorial on this after an hour or so and got it working using Boolean,

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to know the animator's bones 0 Answers

Script Variable not transfering 0 Answers

RayCast and Animation not working 0 Answers

Animating Sprites Through Separate Script? 0 Answers

Cannot get an animation to play upon key press 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