FirstPersonController.cs and Moving platforms?

Hello,
I have the FirstPersonController.cs script on my character, and designed a moving escalator, but the character does not move along with the stair of the escalator that it is standing on. Is there a way to code it to move while it is standing still on the step, and to also be able to jump and move along with the controls (so that it doesn’t get stuck on the step that it is standing on) both at the same time? If so, what would I have to add to the CS script? I feel I would need to somehow get the object under it that it is standing on, but don’t see a way to do that under the CharacterController script. It only determines if the character is grounded or not, but not what object it is standing on. Would this be a good feature to add?

Thank you,
Michael S. Lowe

There’s a few approaches. Not sure which would be best for you. One way is when you land, check if the object you’re on is marked as moving (tag, layer, or by the presence of a custom script component), or perhaps just see if it has a rigidbody on it.

If it does, then your FPC would make note of that and keep it around until it either steps off that object, or jumps.

Each frame, when you go to move, you would query that object’s position, and use that difference from the last frame to additionally move you. It gets real tricky if the object is also rotating, but I’ve not seen a lot of rotating escalators lately.

Anyway, I’ve not personally solved this problem myself, but I imagine if I had it to solve, I’d do something like that.

I added a function called “MoveAlongPlatforms()” in FirstPersonController.cs…

        private GameObject lastGroundObj;
        private GameObject groundObj;
        private Vector3 lastPlatformPos;
        private void moveAlongPlatforms()
        {
            if (!m_CharacterController.isGrounded)
            {
                groundObj = null;
            }
            else
            {
                Vector3 bottom = transform.position - new Vector3(0.0f, m_CharacterController.height * 1.0f / 2, 0.0f);

                Ray ray = new Ray(bottom, -Vector3.up);
                RaycastHit hit = new RaycastHit();

                Physics.Raycast(ray, out hit);

                if (hit.transform != null)
                {
                    groundObj = hit.transform.gameObject;
                }
                else
                {
                    groundObj = null;
                }
            }

            if (groundObj != null)
            {
                Vector3 diff = groundObj.transform.position - lastPlatformPos;

                if (lastGroundObj == groundObj)
                {
                    transform.position += diff;
                }

                lastGroundObj = groundObj;
                lastPlatformPos = groundObj.transform.position;
            }
        }

It doesn’t deal with rotations, but only movements.

Here is what I did in the update() function of FirstPersonController.cs…

private void Update()
        {
            RotateView();
            // the jump state needs to read here to make sure it is not missed
            if (!m_Jump)
            {
                m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
            }

            if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
            {
                StartCoroutine(m_JumpBob.DoBobCycle());
                PlayLandingSound();
                m_MoveDir.y = 0f;
                m_Jumping = false;
            }

            //Function call goes here.
            moveAlongPlatforms();

            if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
            {
                m_MoveDir.y = 0f;
            }
            m_PreviouslyGrounded = m_CharacterController.isGrounded;
        }