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 sovan · Sep 02, 2015 at 09:39 AM · animatortransform.translate

How do I access the animator parameter through C# script ?

Hello friends,

Game: This is a 2D game in which according to the swipe direction(4 cardinal direction) the character will move and reach the destination.

Problem: I want the character to move indefinitely in a particular swipe direction without stopping. But in the following code the character moves only after each swipe(in this case I have tried only for right swipe just to test) and not continuously.

// Update is called once per frame void Update () { //Check if Input has registered more than zero touches if (Input.touchCount > 0) { //Store the first touch detected. Touch myTouch = Input.touches[0];

         //Check if the phase of that touch equals Began
         if(myTouch.phase == TouchPhase.Began)
         {
             //If so, set touchOrigin to the position of that touch
             touchOrigin = myTouch.position;
         }

         //If the touch phase is not Began, and instead is equal to Ended and the x of touchOrigin is greater or equal to zero:
         else if(myTouch.phase == TouchPhase.Ended && touchOrigin.x >= 0)
         {
             //store the postion of the touch into touchEnd vector2 
             Vector2 touchEnd = myTouch.position;

             //Calculate the difference between the beginning and end of the touch in x-axis
             float x = touchEnd.x - touchOrigin.x;

             //Calculate the difference between the beginning and end of the touch in y-axis
             float y = touchEnd.y - touchOrigin.y;

             //touchOrigin.x to -1 so that the next frame should not execute the if else
             touchOrigin.x = -1;

             //check if it is a horizontal(x axis is greater) or vertical(y axis is greater) swipe
             if(Mathf.Abs(x) > Mathf.Abs(y))
             {
                 //if x is greater than than 0, set horizontal value to 1 else -1
                 horizontal = x > 0 ? 1 : -1;
                 if (horizontal == 1)
                 {
                     animator.SetInteger("direction" , 4);
                     int dir = animator.GetInteger("direction");
                     if(dir == 4)
                         gameObject.transform.Translate(Vector2.right * 4.0f * Time.deltaTime);

                 }
                 else if (horizontal == -1)
                 {
                     animator.SetInteger("direction" , 3);
                 }
             }
             else
             {    vertical = y > 0 ? 1 : -1;
                 if (vertical == 1)
                 {
                     animator.SetInteger("direction" , 1);
                 }
                 else if (vertical == -1)
                 {
                     animator.SetInteger("direction" , 2);
                 }
             }
         }
     }

 }![alt text][1]

Please tell me how do I access the animator parameter "direction" values in this case to use conditions in the script. Please find the attachment of the animator.

[1]: /storage/temp/53297-animator.png

animator.png (70.9 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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

2D Animation does not start 1 Answer

Changing Animator State during run-time? 0 Answers

Animation clip 'BackgroundAnim' is not retargetable? 0 Answers

Animator says that animation is playing when it's not. 0 Answers

Animator.Initialize causes huge hitches in-game..preloadable? 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