Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!