Smooth Multi-directional Scrolling

Hi chaps and chapesses!

I’ve been scratching my head over this for some time now and a search of the forums (and Google in general) has not provided me with an answer (or at least, one that I recognise as an answer!)

The scrolling works - I can increase the speed as desired by pressing ‘A’ (only testing on one direction thus far), but there is a noticeable ‘skip’ when the newPositionX resets to 0. (15 is the width of my tile I am er, tiling) and I need to reposition back at 0 when it reaches 15. Could someone kindly advise what I need to tweak to achieve this?

I’m thinking that my ‘IF’ statements might be overkill, having used mathf.repeat?

Look forward to any assistance - it would be greatly appreciated!!

My full class, in its current (working but jittery) state:

using UnityEngine;
using System.Collections;

public class MoverScroller : MonoBehaviour
{
    public float scrollSpeedX; //The speed that the player is moving in the X direction
    public float scrollSpeedZ; //The speed that the player is moving in the Z direction
    public float maxSpeedX; //The maximum speed that the player can move in the X direction
    public float maxSpeedZ; //The maximum speed that the player can move in the Z direction
    public float tileSizeX; //The X size of the background tiles 
    public float tileSizeZ; //The Z size of the background tiles

    private Vector3 startPosition; //The start position of the player
    private float newPositionX;
    private float newPositionZ;

    void Start ()
    {
        startPosition = transform.position;
    }
   
    void Update ()
    {
        GetPlayerInput();

        if (newPositionX <= tileSizeX)
        {
            newPositionX += Mathf.Repeat(Time.deltaTime * scrollSpeedX, tileSizeX);
        }
        else
            newPositionX = 0;

        if (newPositionZ <= tileSizeZ)
        {
            newPositionZ += Mathf.Repeat(Time.deltaTime * scrollSpeedZ, tileSizeZ);
        }
        else
            newPositionZ = 0;

        transform.position = startPosition + (Vector3.forward * newPositionZ) + (Vector3.left * newPositionX);
        Debug.Log(newPositionX);
    }

    void GetPlayerInput()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            scrollSpeedX += 1;
        }
    }
}

Update: The code above didn’t work when I tested it with changing:

  • void GetPlayerInput()
  • {
  • if(Input.GetKeyDown(KeyCode.A))
  • {
  • scrollSpeedX += 1;
  • }
  • }

to

  • void GetPlayerInput()
  • {
  • if(Input.GetKeyDown(KeyCode.A))
  • {
  • scrollSpeedX -= 1;
  • }
  • }

…it went all flickery and wouldn’t scroll.

Reverting back to my initial setup which DID work (scrolls all directions), but jumps significantly upon changing any of the speed variables. (Update routine only, rest same as above):

    void Update ()
    {
        GetPlayerInput();

        float newPositionX = Mathf.Repeat(Time.time * scrollSpeedX, tileSizeX);
        float newPositionZ = Mathf.Repeat(Time.time * scrollSpeedZ, tileSizeZ);

        transform.position = startPosition + (Vector3.forward * newPositionZ) + (Vector3.left * newPositionX);
        Debug.Log(newPositionX);
    }