- Home /
Scroll Ball Texture Smoothly
I want to scroll ball texture smoothly based on game player input, by default vertical scrolling and based on game player finger movement, little bit horizontal scrolling as well.
Mostly like this game kind of implementation: Catch up - Ketchapp
I have tried to write something from my side but didn't able to get satisfactory result. BallHorizontalInputHandler Script:
public class BallInputHandler : MonoBehaviour
{
private Vector3 lastTouchPos;
private Vector3 currTouchPos;
private bool isTouchRunning;
//
[HideInInspector]
public float horizontalInput;
public float smoothnessValue;
void Update ()
{
if (Input.GetMouseButtonDown (0)) {
lastTouchPos = Camera.main.ScreenToViewportPoint (Input.mousePosition);
isTouchRunning = true;
} else if (Input.GetMouseButtonUp (0)) {
isTouchRunning = false;
}
if (isTouchRunning) {
currTouchPos = Camera.main.ScreenToViewportPoint (Input.mousePosition);
horizontalInput = (currTouchPos.x - lastTouchPos.x) * smoothnessValue;
// horzGestureDiff = horzGestureDiff > 0.5f ? 0.5f : horzGestureDiff;
lastTouchPos = currTouchPos;
} else
horizontalInput = 0f;
}
}
BallController Script:
void Update ()
{
if (!GameManager.Instance.IsGameRunninng)
return;
// ball texture rolling
textureOffset.x -= myRigidBody.velocity.normalized.z * (speed / 500f);
textureOffset.y = myRigidBody.velocity.normalized.x * (speed / 30f);
myMaterial.mainTextureOffset = Vector2.Lerp (myMaterial.GetTextureOffset ("_MainTex"), textureOffset, speed * Time.deltaTime);
}
Based on ball's current velocity, I have updated ball speed. Now give me some of your side useful suggestions so I can implement it and scroll ball properly.
Comment