How to make infinite floor using 2 cube object

hi please help me i want to make floor move when player also move at the same direction
and create an infinite runner type game but only using 2 cube object

`

https://drive.google.com/file/d/1v57RU7f4-5ewkHtvbWdduT9sYLnWBmOQ/view

`

So you have a test and you want us to answer the test for you?

ah maybe a little help because thats the only part that im confused
but its okay if you don’t want to

I want to, I just don’t want to have you misrepresent yourself in a job interview.

In addition, there are links in the document with videos of what they want but the links don’t work. So, I even if I did help, I wouldn’t necessarily be giving you the correct information

that is the criteria for the floor panel and im confused on how to make the panel switch between each other

You have to determine which direction the character is moving in, and on which panel it’s currently on. Then move the other panel to the side it’s moving to.

your_test
Something like this

thank you
can i know what classes or function you use for that?

I used triggers on the edges and handled the OnTriggerEnter to move the platforms. I don’t want to give you more than that because a) if you don’t know how to do it with triggers then you would be misrepresented, and b) I slapped this together in a few minutes so it’s horrible code

thank you sir for your help

I thought about it and realised that if you didn’t add the document I would’ve helped you without thinking about it twice. Also, even if you didn’t know how to do it, I could explain it nicely (I hope) and then you would learn how to do it (I hope) and would actually know in the future (I hope)

So, I cleaned up the code a little - and changed it because the doc says the floors ‘rotate’ underneath each other - and here’s what I did (Don’t just copy-and-paste all of this. Read the comments and understand it):

I set up a floor with 2 colliders marked as triggers and made a prefab

Floor (not the root Floor, the first child) here is just a cube. I have changed nothing on it except to set the y-scale to 0.1

Left Trigger and Right Trigger are empty game objects with a Box Collider - marked as trigger - on each. They are this tall because my character can jump and will jump over the triggers if they are too short. Each also has a FloorTrigger script which I will get to later

Target under each trigger is just an empty game object. They are positioned where the other floor should be when in that position. This helps us define where we want the other floor to go
image

These are the floors in the scene

I changed the material on each floor in the scene. Each trigger on each floor has a reference to the other floor. This could probably have been done better. But I didn’t do it better.
image

In my case I have the Right Trigger's Invert checked.

The FloorTrigger script handles each individual trigger. It gets a reference to it’s own root, and the Target transform underneath it. We give it a reference to the other floor in the scene. We can also set the speed at which it should rotate and we have an ‘Invert’ flag because the rotation should be inverted for one of the directions. We will just Lerp backwards (from 1 to 0) in that case.

It then waits for something to enter the trigger. When something enters the trigger and it is not the player it will do nothing. It will also do nothing if the floor is already where we want it to be. If it is the player and the floor is not where we want it to be, we start a coroutine to transition the floor to the position we want.

The coroutine does some calculations and then moves the floor.

We calculate the radius because we want the ‘other’ floor to rotate around this floor. We then use some trigonometry to determine the location of the ‘other’ floor at a specific time during the transition. Then we also rotate the floor so that it appears to be ‘stuck’ to that pivot point. When the transition is done, we will reset the rotation and position to be the same as the ‘Target’

That’s it. Job’s done.

This is what that looks like
your_test

And here’s the FloorTrigger script with comments

using System.Collections;
using UnityEngine;

public class FloorTrigger : MonoBehaviour
{
    // Reference to the other floor
    [SerializeField] Transform _otherFloor;
    [Header("Floor Transition")]
    // Speed to transition. Will need to be fine-tuned for the speed of the character
    [SerializeField] float _speed = 0.125f;
    // Invert is required because in one direction we need to 'reverse' the transition
    [SerializeField] bool _invert;

    // Reference to this floor
    private Transform _myFloor;
    // Reference to the position this trigger wants to put the other floor
    private Transform _otherFloorTarget;
    // Reference to the coroutine that rotates the floor
    private Coroutine _rotateRoutine;

    private void Awake()
    {
        // Get a reference to my floor
        _myFloor = transform.root;
        // Get a reference to the other floor's target
        _otherFloorTarget = transform.GetChild(0);
    }

    private void OnTriggerEnter(Collider other)
    {
        // If it's not the player that hit the trigger, do nothing
        if (!other.CompareTag("Player")) return;

        // If the other floor is already where we want it, do nothing
        if (_otherFloor.position == _otherFloorTarget.position) return;

        // Rotate the floor to the new position
        RotateFloor();
    }

    private void RotateFloor()
    {
        // If the coroutine is currently running, stop it. We are doing a new one
        if (_rotateRoutine != null) StopCoroutine(_rotateRoutine);
        // Start the coroutine and store a reference to it
        _rotateRoutine = StartCoroutine(Routine());

        // The actual coroutine
        IEnumerator Routine()
        {
            // Get the distance between floors to act as a 'pivot'
            var radius = (_otherFloor.position - _myFloor.position).magnitude;

            // Loop until we've completed the rotation at the required speed
            for (var timer = 0f; timer / _speed <= 1f; timer += Time.deltaTime)
            {
                // Get the current 'time' of the rotation, taking 'invert' into account
                var time = timer / _speed;
                if (_invert) time = 1f - time;
                // Get the angle (in radians) at the current 'time'
                var angle = Mathf.LerpAngle(90f, -90f, time) * Mathf.Deg2Rad;
                // Calculate the x and y position of the other floor at this 'time'
                var x = Mathf.Sin(angle) * radius;
                var y = Mathf.Cos(angle) * radius;
                // Position the other floor relative to this floor
                _otherFloor.position = _myFloor.position + new Vector3(x, y, 0f);
                // Rotate the floor to the correct rotation for this position
                _otherFloor.rotation = Quaternion.LookRotation((_otherFloor.position - _myFloor.position).normalized);
                // Take a breath
                yield return null;
            }

            // Set the position and rotation to _exactly_ what is expected
            _otherFloor.position = _otherFloorTarget.position;
            _otherFloor.rotation = _otherFloorTarget.rotation;

            // We are done. No need to keep the routine reference
            _rotateRoutine = null;
        }
    }
}

Now if I also apply for that job, one of us is going to be in trouble…

woah so nice of you
thank you very much
maybe you should apply for the job too hahaha

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms