0
var steps_container = $("#steps")[0];

setTimeout(myFunction, 3000);
setInterval(frameUpdate, 50);


var n = 30;
var i=0;
var steps = [];
var steps_x = [];
for (i=0;i<n;i++)
{

    var step = document.createElement("div");

  step.id = "step";
  var step_x = i * 24;
  step.style.left =  step_x.toString() + "px";

  step.style.top = "10px";
  steps[i] = step;
  steps_x[i] = step_x;
  steps_container.appendChild(step);    
}


function frameUpdate()
{
    setStepsPosition();
}

function setStepsPosition()
{
  var i;
  for (i=0;i<n;i++)
  {

    var step_x = steps_x[i];
    step_x = step_x + 1;
    var max_x = (n-1)*24;
    if (step_x >  max_x )
    {
      step_x = 0;
    }
    steps_x[i] = step_x;

    steps[i].style.left = step_x.toString() + "px";

    var r = 5;
    var x_for_atan = step_x*r/max_x - (r/2);
    // extra non linear
    if (step_x<3*24)
    {
        x_for_atan = -(r/2) + 0.5;
    }
    if (step_x>=max_x - 3*24)
    {
        x_for_atan = +(r/2) - 0.5;
    }
    var y = 180 + 120 * Math.atan(x_for_atan);
    steps[i].style.top = y.toString() + "px";
  } 
}

function myFunction() {
  //  alert('Hello');
}


// handle click and add class
button.on("click", function(){
  banner.addClass("alt")
})

Taken from this jsfiddle:

Javascript jquery

The idea is to move objects one by one each one following the other one. In unity I have part of a code I did already but not sure how to convert and use this javascript jquery. My brother did this javascript jquery sample.

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

public class MoveObjects : MonoBehaviour
{
    public float speed = 3f;

    private GameObject[] objectstoMove;
    private List<GameObject> objectsMoving = new List<GameObject>();

    // Use this for initialization
    public void Init()
    {
        objectstoMove = GameObject.FindGameObjectsWithTag("Stair");
        objectsMoving = new List<GameObject>(objectstoMove);
    }

    // Update is called once per frame
    void Update()
    {
        if (objectstoMove != null)
        {
            float step = speed * Time.deltaTime;
            for (int i = 0; i < objectstoMove.Length; i++)
            {
                objectsMoving[i].transform.Translate((objectsMoving[i].transform.up + objectsMoving[i].transform.forward) * step);
            }
        }
    }
}

I can't figure out how to implement the javascript/jquery code with the unity/csharp code.

UPDATE: What I'm trying to is simulate stairs escalator. My first script create and position stairs:

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

public class GenerateStairs : MonoBehaviour
{
    public GameObject stairsPrefab;
    public float delay = 3;
    public int stairsNumber = 5;
    public Vector3 stairsStartPosition;
    public Vector3 stairSize;
    public Vector3 stairsSize;
    public float stepWidthFactor = 1f;
    public MoveObjects moveobjects;

    private Vector3 stairsPosition;
    private GameObject stairsParent;

    // Use this for initialization
    void Start()
    {
        stairsParent = GameObject.Find("Stairs");
        StartCoroutine(BuildStairs());
    }

    // Update is called once per frame
    void Update()
    {

    }

    private IEnumerator BuildStairs()
    {
        for (int i = 1; i <= stairsNumber; i++)
        {

            stairsPosition = new Vector3(
                    stairsStartPosition.x,
                    stairsStartPosition.y + (i * stairsSize.y),
                    stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

            GameObject stairs = Instantiate(
                    stairsPrefab,
                    stairsPosition,
                    Quaternion.identity);

            stairs.tag = "Stair";
            stairs.transform.parent = stairsParent.transform;
            stairs.transform.localScale = stairSize;

            yield return new WaitForSeconds(delay);
        }

        moveobjects.Init();
    }
}

Stairs

Then I want to use the MoveObjects script to make the stairs escalator effect.

3
  • 1
    Is there a particular problem you're having converting the javascript into C#? It looks like pretty much the only thing you need is the stuff in SetStepsPosition() and you can put the equivalent of that in Update. Commented Nov 16, 2018 at 23:22
  • @Ruzihm Ok I will try. What is steps[i].style ? What is the style stand for ? Or what are the steps ? My guess is that I don't need this line ? steps[i].style.left = step_x.toString() + "px"; Commented Nov 16, 2018 at 23:27
  • style is how the javascript is changing the position of the steps. You'll want to change step.transform.position.x/y/z instead of changing step.style.left/top. Commented Nov 16, 2018 at 23:28

1 Answer 1

1

Pretty much put the contents of SetStepsPosition() into Update() and set the transform.position of the step where the javascript changes the style.left/top CSS of the step.

I would recommend using Mathf.Repeat here to handle the wrapping of the x-coordinate of the step, to avoid misaligning the steps.

void Update() {
    float n = (float) objectsMoving.Count;

    foreach (GameObject step in objectsMoving) {
        float step_x = step.transform.position.x;
        float max_x = (n-1f) * 24f; // The javascript has this "24", which is based on the width of the step + the outline.
        step_x = Mathf.Repeat(step_x + speed * Time.time, max_x);

        float r = 5f;
        float x_for_atan = step_x * r / max_x - r/2f;
        if (step_x < 3f * 24f) {
            x_for_atan = -r/2f +.5f;
        }
        if (step_x >= max_x - 3f * 24f) {
            x_for_atan = r/2f - .5f;
        }

        float step_y = 180f + 120f * Math.Atan(x_for_atan);

        step.transform.position = new Vector3(step_x,-step_y,0f); 
        // negative y because the javascript uses top, which starts at 0
        // at the top, and positive goes down. Unity goes the other direction.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.