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 Vidamo · Nov 17, 2014 at 03:14 PM · androidspritetouchtouchscreensprite animation

I can't change the sprite using the Touch class

I want my player sprite to run from left to right when tapped from left part of the screen or the right part of the screen. I have a code for Touch input and for keyboard input. I think both of these classes has the same logic so I did this.


FOR TOUCH INPUT...

void Update () {

         float touchSpeed = 10;
         
         float jumpDelaySpeed = 1.1f;
 
     if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             Vector2 touchPosition = Input.GetTouch(0).position;
             double halfScreen = Screen.width / 2.0;
             //Check if it is left or right?
             if(touchPosition.x < halfScreen)
                 {
                 player.transform.Translate(Vector3.left * touchSpeed * Time.deltaTime);
                 graphics.SendMessage("Running");
                 
                 //GameObject.Find("Graphics").SendMessage("Running");    //Change to running animation
                 increase = increase + 1;
                 } 
             else if (touchPosition.x > halfScreen) 
                 {
                 player.transform.Translate(Vector3.right * touchSpeed * Time.deltaTime);
                 graphics.SendMessage("Running");    //Change to running animation
                     Debug.Log ("clicked");
                 }
                  else
                 {
                 graphics.SendMessage("Idle");
                 }
         }

AND for KEYBOARD INPUT..

 if (jumpDelay > 0)                                    //Time count down for the report delay
 {
     for (int i = 0; i < 4; i++)
     {
         jumpDelay -= Time.deltaTime * jumpDelaySpeed;
     }
 }
 else if (jumpDelay <= 0)
 {
     if (Input.GetKey("a"))
     {
     // print("flashes reporting animation")
     // tv patrol theme song plays
     GameObject.Find("Graphics").SendMessage("Running");    //Change to running animation
     //audio.Play(); 
     }
     else if (Input.GetKey("d"))
     {
         GameObject.Find("Graphics").SendMessage("Running");
         //audio.Stop();
     }
     else
     {
         GameObject.Find("Graphics").SendMessage("Idle");
         //audio.Stop();
     }

     if (Input.GetButton("Jump"))
     {
         {
             GameObject.Find("Graphics").SendMessage("PlayerJump");
             jumpDelay = jumpDelay + 6;
         }
     }
 }



They are all in the Update function. When I play it, it works well for the keyboard input but it doesn't work on touch input. When I use the touch input, the gameObject just moves from left to right but the sprite just stands.


This is my AnimateTexture.js code (it executes what sprite in the sprite sheet I should use)

 //vars for the whole sheet
 var colCount    : int =  4;
 var rowCount    : int =  4;
 var number : int = 0;
 //vars for animation
 var rowNumber    : int =  0; //Zero Indexed
 var colNumber    : int =  0; //Zero Indexed
 var totalCells    : int =  4;
 var fps        : int = 10;
 var offset    : Vector2;  //Maybe this should be a private var
 
 
 
 //Update
 function Update () { 
     SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  
 }
 
 
  
 //SetSpriteAnimation
 function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int){
  
     // Calculate index
     var index : int = Time.time * fps;
     // Repeat when exhausting all cells
     index = index % totalCells;
  
     // Size of every cell
     var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
  
     // split into horizontal and vertical index
     var uIndex = index % colCount;
     var vIndex = index / colCount;
  
     // build offset
     // v coordinate is the bottom of the image in opengl so we need to invert.
     offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
  
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale  ("_MainTex", size);
 }
 
 function Running () {
     rowNumber = 2;
     number = 1;
 }
 
 function Report () {
     rowNumber = 1;
 }
 
 function PlayerJump () {
     rowNumber = 3;
 }
 
 function PlayerHurt () {
     rowNumber = 4;
 }
 
 function Idle () {
     rowNumber = 0;
 }


I don't understand. I just used the same logic as the Keyboard input has.. I just need the sprite to run from left to right when tapped. Please help!

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

Using GUI buttons to move a sprite (C#) 2 Answers

Problem with touch button (when moved) 2 Answers

Having an issue with getting my touch controls working 0 Answers

smooth movement with rigidbody2D 1 Answer

How to move an image by tocuhing a button (using UI) [Android game] 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