- Home /
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!
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                