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 kayb14 · Jun 20, 2016 at 07:43 AM · animationmecanimmovement scriptartificial intelligencebehaviour

mecanim animation while bool=true? Problem: animation restarts every frame! C#

Hey there, I'm a quite new to game-coding, but after coding all the gameplay elements of my horror-game I started creating a dirty and simple AI-system using mecanim animations and here comes the problem.

Codesnippets below the explanaition!

Problem: the mecanim animations for the enemy restart every-frame

Setup:

  • the capsule has my "aisystem"-script to move/attack/idle the position of the enemy and the "lookattarget"-script to get the players direction.

  • the two sphere-collider "WALKRANGE"(scale(4/4/4)) & "ATTACKRANGE"(scale(2/2/2)) have a simple OnTriggerEnter/exit-script wich correspond with the "aisystem"-script.

  • unitychan's animations get called through booleans in the update() of the "aisystem"-script

Desired Outcome: the Capsule moves the unity-chan model while she plays corresponding animations (walk forward / attack / idle)

I really appreciate any kind of help! Since my Idea was to call the animations in a while-loop in the update() :D :D :D If you know any fitting tutorials, if possible readable ones, be sure to post them! All necessary pictures and codesnippets are added below!

and sry for my english,

Kim

Hierachysetup

hierachysetup

alt text

aisystem (move capsule with children & trigger enemymodel's animations)

 using UnityEngine;
 using System.Collections;
 
 public class aisystem : MonoBehaviour {
 
     public GameObject enemy;
     public GameObject enemymodel;
     public bool move = false;
     public bool attack = false;
     public float smooth = 1;
 
     public Animator enemymovement;
     // Use this for initialization
     void Start () {
         enemymovement = enemymodel.GetComponent<Animator>();
     }
 
     public void StartMovement()
     {
         move = true;
     }
     public void StopMovement()
     {
         move = false;
     }
     public void StartAttack()
     {
         attack = true;
     }
     public void StopAttack()
     {
         attack = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(move==true)
         {
             enemy.transform.Translate(Vector3.forward * smooth * Time.deltaTime);
             enemymovement.Play("WALK00_F");
         }
         if (attack==true)
         { enemymovement.Play("WAIT04"); }
          if (move==false)
         {
             enemymovement.Play("WAIT01");
         }
     }
 }

trigger movement

 using UnityEngine;
 using System.Collections;
 
 public class aimove : MonoBehaviour {
 
     public GameObject enemy;
 
     // Use this for initialization
     void Start () {
         
 
     }
 
     void OnTriggerEnter(Collider moverange)
     {
         if (moverange.tag == "Player")
         {
             enemy.transform.GetComponent<aisystem>().StartMovement();
         }
     }
     void OnTriggerExit(Collider moverangeout)
     {
         if (moverangeout.tag == "Player")
         {
             enemy.transform.GetComponent<aisystem>().StopMovement();
         }
     }
 
     // Update is called once per frame
     void Update () {
         
     }
 }



trigger attack

 using UnityEngine;
 using System.Collections;
 
 public class aiattack : MonoBehaviour {
     public GameObject enemy;
     // Use this for initialization
     void Start () {
     
     }
 
     void OnTriggerEnter(Collider attackrange)
     {
         if (attackrange.tag == "Player")
         {
             enemy.transform.GetComponent<aisystem>().StartAttack();
             enemy.transform.GetComponent<aisystem>().StopMovement();
         }
     }
     void OnTriggerExit(Collider attackrangeout)
     {
         if (attackrangeout.tag == "Player")
         {
             enemy.transform.GetComponent<aisystem>().StopAttack();
             enemy.transform.GetComponent<aisystem>().StartMovement();
         }
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 }
 


hierachy.png (450.9 kB)
animator.png (27.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

Answer by kayb14 · Jun 20, 2016 at 08:50 AM

It works just fine if I cut out the transitions in the animatior, if not it switches between states and transitions almost each frame

Comment
Add comment · 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

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

102 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

Related Questions

Humanoid Animations 0 Answers

How do I remove the latency/delay/pause when starting a clip with animator.Play()? 0 Answers

Sync Anima2D Animations with Player Movement 0 Answers

Mecanim Generic No movement 0 Answers

´How to check if 3D player is moving? 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