Search Unity

Question How to trigger platform ?

Discussion in 'Getting Started' started by t3bt2030, Jul 29, 2021.

  1. t3bt2030

    t3bt2030

    Joined:
    Jul 29, 2021
    Posts:
    1
    hi , i am beginner with unity and coding . i want my character when he step on the platform the platform will move , right and left , or up and down .
    i watched video on how to make the platform move constantly . but i did not find what i am looking for
     
  2. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    481
    This is an example using the linked script for Moving Platforms Horizontal and Vertical 2D (008.pdf @ rp-interactive.nl) and provided by Unity forum member @Realspawn1, for which many thanks.

    Edit the script as follows:

    Add variable:

    Code (CSharp):
    1.     private bool isOnPlatform;
    Create two new methods to trigger when Player is on the Platform and when the Player is not on the Platform:

    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject.CompareTag ("Player"))
    4.             {
    5.                 isOnPlatform = true;
    6.             }
    7.     }
    8.  
    9.     private void OnTriggerExit2D(Collider2D collision)
    10.     {
    11.         if (collision.gameObject.CompareTag("Player"))
    12.             {
    13.                 isOnPlatform = false;
    14.             }
    15.     }
    Make sure the Player Game Object is tagged as Player in Inspector.

    Make sure that the Platform Box Collider 2D “Is Trigger” field is checked in Inspector.

    Create a new method:

    Code (CSharp):
    1.     private void MovePlatform()
    2.     {
    3.  
    4.     }
    Cut code from Update() method and paste it in MovePlatform() method.

    In the Update() method, call the MovePlatform() method when Player is on the platform, as follows:

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         if (isOnPlatform == true)
    4.         {
    5.             MovePlatform();
    6.         }    
    7.     }
    I hope this helps you.
     
    Last edited: Aug 6, 2021
  3. jandju22

    jandju22

    Joined:
    Jul 29, 2023
    Posts:
    1
    Eyo thats pretty dope, but how would I make it so that it just activates after im on the platform. For example, the platform isnt moving, i jump on it, it moves, I jump off it, it continues to move.