Interactive Novel based on a scroll-snap-gallery for 3D objects

Hey guys,

I’m trying to create an interactive novel for tablets in Unity. There are several islands which all show different parts of the plot. The story is told by swiping from one island to the next one. There is always one island in the center of the device. I want the application to basically function like a scroll-snap-gallery for 3D objects where only the center island and the islands directly next to it (on the left and the right side) are set active. I obviously need the islands to appear in a certain order when they get swiped in. I’m quite new to Unity and coding is not my strength so far – unfortunately all I can find about this topic are tutorials for UI elements. Any help is highly appreciated.

Thanks in advance!

This is my code so far but it doesn’t work properly since it’s only working for one direction and I don’t know how to make my prefabs appear in a certain order:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwipeControlHorizontal : MonoBehaviour
{
    enum Positions
    {
        StartPos,
        MidPos,
        EndPos
    };


    Positions positions;



    float speed = 10.0f;
    public GameObject island;



    public Vector3 startPoint;
    public Vector3 mainPoint;
    public Vector3 endPoint;

    public float minMoveDist = -6.0f;

    public List<GameObject> islandPrefabs = new List<GameObject>();
    bool newPosIsChosen = false;
  

    float endDist;

   


    // Use this for initialization
    void Start()
    {
        //islandPrefabs[1].SetActive(true);
    }

    void Update()
    {

        if (transform.position.x == startPoint.x)
        {
            positions = Positions.StartPos;
            newPosIsChosen = false;
        }
           

        if (transform.position.x == mainPoint.x)
        {
            positions = Positions.MidPos;
            newPosIsChosen = false;
        }
           

        if (transform.position.x == endPoint.x){
            positions = Positions.EndPos;
            //Destroy(gameObject);

        }
           




        SwipeMove();
        FinalPositionMovement();


        if(Input.GetMouseButtonUp(0))
        {
            //newPosIsChosen = false;
        }
        //Camera.main.ScreenToWorldPoint();

       // Debug.Log(gameObject.name + endDist);
    }

    void FinalPositionMovement(){
       
        if (Input.GetMouseButtonUp(0))
        {
            if (positions == Positions.StartPos)
            {
                endDist = mainPoint.x - transform.position.x;
            }

            if (positions == Positions.MidPos)
            {
                endDist = endPoint.x - transform.position.x;
            }

            if (endDist >= minMoveDist)
            {
                newPosIsChosen = true;
            }

           // Debug.Log(gameObject.name + " " + endDist);

        }

       // Debug.Log(gameObject.name + " " + newPosIsChosen);
    }


    void SwipeMove(){
       
        if (Input.GetMouseButton(0))
        {
            if (Input.GetAxis("Mouse X") > 0)
            {
                island.transform.position += new Vector3(Input.GetAxisRaw("Mouse X") * Time.deltaTime * speed, 0.0f, 0.0f);
            }

            else if (Input.GetAxis("Mouse X") < 0)
            {
                island.transform.position += new Vector3(Input.GetAxisRaw("Mouse X") * Time.deltaTime * speed, 0.0f, 0.0f);
            }


        }

        if(!Input.GetMouseButton(0)){

            if(newPosIsChosen)
            {
                if(positions == Positions.StartPos)
                {
                    transform.position = Vector3.MoveTowards(transform.position, mainPoint, .51f);
                }

                if (positions == Positions.MidPos)
                {
                    transform.position = Vector3.MoveTowards(transform.position, endPoint, .51f);
                }

                if (positions == Positions.EndPos)
                {
                   
                }
            }
            else
            {
                if (positions == Positions.StartPos)
                {
                    transform.position = Vector3.MoveTowards(transform.position, startPoint, .51f);
                }

                if (positions == Positions.MidPos)
                {
                    transform.position = Vector3.MoveTowards(transform.position, mainPoint, .51f);
                }

                if (positions == Positions.EndPos)
                {

                }
            }
        }

    }

  

}

You could set up empty gameobject positions for each object and the move the camera right and left using MoveTowards. If the empties were in an array, it would be easy to move right or left by just moving to the next index.

1 Like