- Home /
 
               Question by 
               GeneiRyodan · Oct 29, 2017 at 05:15 PM · 
                touchspeedchangetouchcount  
              
 
              Change speed on Touch
So this code scrolls background infinitely. I have a problem in the Touch part. The scrollSpeed slows down each time when i tap. I want it to slow down the scrollSpeed on tap and when it slowed, go to initial scrollSpeed on a next tap. There is the problem inside the for loop. Please help. Thanks in advance!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BackgroundScroller : MonoBehaviour {
 [SerializeField]
 private float scrollSpeed;
 [SerializeField]
 private float tileSizeZ;
 private Vector2 saveOffset;
 private Vector3 startPosition;
 private bool isSlowed = false;
 // Use this for initialization
 void Start ()
 {
     startPosition = transform.position;
     saveOffset = GetComponent<Renderer>().sharedMaterial.GetTextureOffset("_MainTex");
 }
 
 // Update is called once per frame
 void FixedUpdate ()
 {
     float x = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ * 4);
     x = x / tileSizeZ;
     x = Mathf.Floor(x);
     x = x / 4;
     Vector2 offset = new Vector2(saveOffset.x, saveOffset.y);
     GetComponent<Renderer>().sharedMaterial.SetTextureOffset("_MainTex", offset);
     float newPosition = Mathf.Repeat(Time.time * scrollSpeed, tileSizeZ);
     transform.position = startPosition + Vector3.up * newPosition;
     //Touch part
     for (int i = 0; i < Input.touchCount; i++)
     {
         if (Input.GetTouch(i).phase == TouchPhase.Began && isSlowed == false)
         {
             scrollSpeed = scrollSpeed / 2;
             isSlowed = true;
         }
         else if (Input.GetTouch(i).phase == TouchPhase.Began && isSlowed == true)
         {
             scrollSpeed = scrollSpeed;
             isSlowed = false;
         }
     }
 }
 void OnDisable()
 {
     GetComponent<Renderer>().sharedMaterial.SetTextureOffset("_MainTex", saveOffset);
 }
}
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                