Search Unity

Creating sofa selection menu with previous and next buttons

Discussion in 'Scripting' started by vannessloh, Feb 8, 2018.

  1. vannessloh

    vannessloh

    Joined:
    Dec 4, 2017
    Posts:
    16
    Hey guys I was created a sofa menu using Vector3.Lerp, I have created five transforms and attached each to the main camera, each transforms will be called by five different UI buttons. But what I want to do is actually control each transforms with only two buttons which are previous and next buttons. Anyone can help?

    Below is the script I created:


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class SofaModelsView : MonoBehaviour
    {
    public Transform[] views;
    public float transitionSpeed;
    Transform currentView;
    // Use this for initialization
    void Start()
    {
    currentView = views[0];
    }
    void LateUpdate()
    {
    //Lerp position
    transform.position = Vector3.Lerp(transform.position, currentView.position, Time.deltaTime * transitionSpeed);
    Vector3 currentAngle = new Vector3(
    Mathf.LerpAngle(transform.rotation.eulerAngles.x, currentView.transform.rotation.eulerAngles.x, Time.deltaTime * transitionSpeed),
    Mathf.LerpAngle(transform.rotation.eulerAngles.y, currentView.transform.rotation.eulerAngles.y, Time.deltaTime * transitionSpeed),
    Mathf.LerpAngle(transform.rotation.eulerAngles.z, currentView.transform.rotation.eulerAngles.z, Time.deltaTime * transitionSpeed));
    transform.eulerAngles = currentAngle;
    }
    public void SofaModel1View()
    {
    currentView = views[0];
    }
    public void SofaModel2View()
    {
    currentView = views[1];
    }
    public void SofaModel3View()
    {
    currentView = views[2];
    }
    public void SofaModel4View()
    {
    currentView = views[3];
    }
    public void SofaModel5View()
    {
    currentView = views[4];
    }
    }
     
  2. laxbrookes

    laxbrookes

    Joined:
    Jan 9, 2015
    Posts:
    235
    From what I gather about this it might be a better idea to create "templates" (prefabs) of each of the view models and store them in a collection. All you will need to do is assemble each of your views once at start up, assign each of them an index and use your current index to cycle through them (showing, hiding or moving) as needed. Happy to help if this requires more explanation.
    It'll help to prevent having methods that explicitly state which view you require.
     
  3. vannessloh

    vannessloh

    Joined:
    Dec 4, 2017
    Posts:
    16
    Thanks for your reply @Malleck666, can you explain more further about this? Is that work together with Vector3.Lerp?