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 PhoenixFeather · Oct 24, 2016 at 11:58 AM · animationscripting beginner

animations controlling

Hi, I'm a beginner in Unity and i met with a problem.

So I have an object - pigeon. It has two animations - 'fly' and 'turn'. 'fly' is a default animation that should play on loop, while 'turn' is an animation that should play when, well, player moves in the opposite direction(its basically the animation of pigeon performing the U-turn).

Now, the problem is my lack of C# knowledge and extreme tiredness. I need my bird to turn in the opposite direction when 'turn' animation ends, but i dont know how. Right now it turns immediatly and at the end of 'turn' animation it turns another time. English isn't my main language so please excuse me for whatever mistakes i made, thanks.

here's image if that helps

here's a code:

 using UnityEngine;
 using System.Collections;
 
 public class moving : MonoBehaviour {
 
     public float movementSpeed = 10;
     private bool canTurn = true;
     private bool canMove = true;
 
     //something i took from tutorial
     Animator myAnim;
     int turnHash = Animator.StringToHash("turn");
 
     void Start ()
     {
         myAnim = GetComponent<Animator>();
     }
 
     void Update()
     {    
         //check if 'turn' animation is playing. If it does, pigeon cant move
         if (myAnim.GetCurrentAnimatorStateInfo (0).IsName ("turning")) {
             canMove = false;
         } else {
             canMove = true;
         }
         //check if... uhhh... I think it checks if 'turn' animation is finished playing
         if (myAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 1 && !myAnim.IsInTransition (0) && myAnim.GetCurrentAnimatorStateInfo (0).IsName ("turning")) {
             canTurn = false;
         } else {
             canTurn = true;
         }
         if (canMove == true) {
             if (Input.GetKey ("right") && canMove == true && !Input.GetKey ("left")) {
                 //if pigeon is facing the left and he can turn, play 'turn' animation
                 if (transform.eulerAngles.y == 0 && canTurn == true) {
                     myAnim.SetTrigger (turnHash);
                 }
                 //if it can move, it moves(and rotates)
                 //if (myAnim.GetCurrentAnimatorStateInfo (0).normalizedTime > 1 && !myAnim.IsInTransition (0)) {
                 //}
                 transform.Translate (Vector3.back * movementSpeed * Time.deltaTime);
                 transform.eulerAngles = new Vector3 (0, 180, 0);
             }
             //pretty much the same, but for left key press
             else if (Input.GetKey ("left") && canMove == true && !Input.GetKey ("right")) {
                 if (transform.eulerAngles.y == 180 && canTurn == true) {
                     myAnim.SetTrigger (turnHash);
                 }
                 //if (canTurn == false) {
                 //}
                 transform.eulerAngles = new Vector3 (0, 0, 0);
                 transform.Translate (Vector3.back * movementSpeed * Time.deltaTime);
             }
         }
     }
 }

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 PhoenixFeather · Oct 24, 2016 at 01:33 PM

So i fixed most of the problems, only the biggest one is left - i still need my bird to

 transform.eulerAngles = new Vector3 (0, 0, 0);

after the 'turning' animation finishes. How do i track when animation is finished?

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 PhoenixFeather · Oct 25, 2016 at 05:17 AM 0
Share

Someone, please help me. I tried doing it like this

 if (myAnim.GetCurrentAnimatorStateInfo (0).IsName ("turning") && myAnim.GetCurrentAnimatorStateInfo (0).normalizedTime >= 1) {
             print ("animation finished");
             if (transform.eulerAngles.y == 0) {
                 transform.eulerAngles = new Vector3 (0, 180, 0);
                 print ("turned to the left");
             }
             else if (transform.eulerAngles.y == 180) {
                 transform.eulerAngles = new Vector3 (0, 0, 0);
                 print ("turned to the right");
             }
         }

but it's not working

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Is it possible to obtain velocity relative to rotation? 0 Answers

creating animation made my each prefab spawn at same position 1 Answer

How to prevent multiple animations playing at the same time on the same object. 1 Answer

How to make a collision script 1 Answer

Animation/Scripting 1 Answer


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