Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 androids · May 02, 2013 at 11:12 AM · animationplay

Play animation C#

Hi Guys, I Have one problem. I have 2 animation. Anim_Left and AnimRight. So my question, this is the script I use in C#. If I swipe to the left, the animation "Anim_Left" has to play, and if I swipe to the right, animation "Anim_Right" needs to play. Someone who can help me out with this problem?

Thanks in advance.

 //C#
 //SwipeHandler.cs
 using UnityEngine;
  
 public class SwipeControl : MonoBehaviour
 {
     public float minMovement = 20.0f;
     public bool sendLeftMessage = true;
     public bool sendRightMessage = true;
     public GameObject MessageTarget = null;
  
     private Vector2 StartPos;
     private int SwipeID = -1;
  
     void Update ()
     {
         if (MessageTarget == null)
             MessageTarget = gameObject;
         foreach (var T in Input.touches)
         {
             var P = T.position;
             if (T.phase == TouchPhase.Began && SwipeID == -1)
             {
                 SwipeID = T.fingerId;
                 StartPos = P;
             }
             else if (T.fingerId == SwipeID)
             {
                 var delta = P - StartPos;
                 if (T.phase == TouchPhase.Moved && delta.magnitude > minMovement)
                 {
                     SwipeID = -1;
                     if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
                     {
                         if (sendRightMessage && delta.x > 0)
                             MessageTarget.SendMessage("OnSwipeRight", SendMessageOptions.DontRequireReceiver);
                         else if (sendLeftMessage && delta.x < 0)
                             MessageTarget.SendMessage("OnSwipeLeft", SendMessageOptions.DontRequireReceiver);
                     }
                 }
                 else if (T.phase == TouchPhase.Canceled || T.phase == TouchPhase.Ended)
                     SwipeID = -1;
             }
         }
     }
     void OnSwipeLeft()
 {
         transform.position -= Vector3.right*80;
 }
     void OnSwipeRight()
 {
         transform.position += Vector3.right*80;
 }
 }
Comment
Add comment · Show 1
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 cj_coimbra · May 02, 2013 at 12:11 PM 0
Share

And what happens with your current code?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TonyLi · May 02, 2013 at 01:30 PM

When starting out with Unity, there's a lot to learn. You'll find a lot of your own answers by reading the documentation and example projects. For example: http://docs.unity3d.com/Documentation/ScriptReference/Animation.html

The documentation will familiarize you with the different animation options. Unity 4 introduced the Mecanim animation system. But for your situation you might find the legacy animation system easier.

This is the scripting reference for the legacy Animation component. If you use it, your game object will need an Animation component. Add "Anim_Left" and "Anim_Right" to the animation list. Then add this code to your script:

 void OnSwipeLeft() {
     transform.position -= Vector3.right*80;
     animation.CrossFade("Anim_Left");
 }
 
 void OnSwipeRight() {
     transform.position += Vector3.right*80;
     animation.CrossFade("Anim_Right");
 }
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 androids · May 02, 2013 at 11:24 PM 0
Share

I had the same script as this you gave me, I had this:

 void OnSwipeLeft() {
     transform.position -= Vector3.right*80;
     animation.Play("Anim_Left");
 }
  
 void OnSwipeRight() {
     transform.position += Vector3.right*80;
     animation.Play("Anim_Right");
 }

But I figured it out, Thanks!

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

14 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

Related Questions

GUITexture touch play animation 0 Answers

Proper way to play non looping animations 2 Answers

can't get fbx animation to play 3 Answers

How to get an object move by itself 2 2 Answers

Ped models do not want to play one animation at the same time 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