Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

304
Vistas
Moving platform in Unity 3D

I need to make a platform that moves up when the player enters it.

MovingPlatform.cs

public class MovingPlatform : MonoBehaviour
{

    [SerializeField]
    private Vector3 newPosition;
       
    void Start()
    {
       newPosition = new Vector3(transform.position.x, transform.position.y + 5, transform.position.z);
      
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            transform.position = Vector3.MoveTowards(transform.position, newPosition, 4 * Time.deltaTime);

        }
    }
}

My problem is that the platform moves up only a little and without the player.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Why the platform moves only once

The reason, the platform moves only once is due to OnTriggerEnter getting called upon entering the trigger not staying inside it. If you want it to get called constantly, while inside it, use OnTriggerStay instead or assign a boolean value inside OnTriggerEnter and OnTriggerExit methods and change your position inside an Update/FixedUpdate.

Why the platform moves without the player

Regarding the platform moving without the player, I'm assuming that the player is a physics object and you expect them to be pushed by it. Potential reasons for that happening:

  • You are moving the platform too fast
  • You are moving the platform outside the physics loop
  • The platform and/or player doesn't have a collider (Trigger colliders do not collide)

Potential solution

It uses OnCollisionEnter instead of the OnTriggerEnter since it seemed to be more appropriate in this case (Make sure to have a non-trigger collider, if you gonna use it).

using UnityEngine;

public class MovingPlatform : MonoBehaviour
{
    [SerializeField]
    private Vector3 targetPosition = default;

    private bool touchingPlayer = false;

    private void Start()
    {
        targetPosition = new Vector3(
            transform.position.x,
            transform.position.y + 5f,
            transform.position.z);
    }

    private void FixedUpdate()
    {
        if (touchingPlayer)
        {
            transform.position = Vector3.MoveTowards(
                transform.position,
                targetPosition,
                4f * Time.fixedDeltaTime);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Player"))
            touchingPlayer = true;
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.gameObject.CompareTag("Player"))
            touchingPlayer = false;
    }
}

https://streamable.com/mbevlw

over 4 years ago · Santiago Trujillo Denunciar

0

Instead Of Moving the platform using Vector3s, Create an animation that plays when the player enters a trigger zone.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda