Skip to Main Content






Unity tutorial

Updating MovePlatform script

We will now make the platform move faster as the score increases, this will add an increasing difficulty to the game.

 

Copy the following code for the MovePlatform script and save the file:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovePlatform : MonoBehaviour
{
    private float speed;
    private Vector2 screenbound;
    public GameObject Score;

    // Start is called before the first frame update
    void Start()
    {
        Score = GameObject.Find("Score");
        screenbound = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, Camera.main.transform.position.z));
    }

    // Update is called once per frame
    void Update()
    {
        /*
         As the speed has to be changed continuously it can't be set at Start() function anymore. 
         */

        speed = 2 + ((float)(Score.GetComponent<score>().scorepoint)) / 30.0f;//this is a suitable equation but change as per your need

        GetComponent<Rigidbody>().velocity = new Vector3(-speed, 0, 0);
        if (transform.position.x < screenbound.x * 2)
        {
            Destroy(this.gameObject);
        }
    }
}