top of page

This is a reference page for Unity Scripts

Marble Run

Controller

Spawner

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

public class PlayerController : MonoBehaviour
{
    public float speed = 15;
    private Rigidbody playerRb;
    private GameObject focalPoint;
    public bool hasPowerUp = false;
    public float powerUpStrength = 15;
    public GameObject powerUpIndicator;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        focalPoint = GameObject.Find("FocalPoint");
    }

    // Update is called once per frame
    void Update()
    {
        

        float forwardInput = Input.GetAxis("Vertical");
        playerRb.AddForce(focalPoint.transform.forward * speed * forwardInput);
        powerUpIndicator.transform.position = transform.position;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("PowerUp"))
        {
            hasPowerUp = true;
            powerUpIndicator.gameObject.SetActive(true);
            Destroy(other.gameObject);
            StartCoroutine(PowerUpCountDown());
        }

    }

    IEnumerator PowerUpCountDown()
    {
        yield return new WaitForSeconds(7);
        hasPowerUp = false;
        powerUpIndicator.gameObject.SetActive(false);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.CompareTag("Enemy") && hasPowerUp)
        {
            Rigidbody enemyRb = collision.gameObject.GetComponent<Rigidbody>();
            Vector3 awayfromPlayer = (collision.transform.position - transform.position);
            enemyRb.AddForce(awayfromPlayer * powerUpStrength, ForceMode.Impulse);
        }
    }
}
 

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

public class Spawner : MonoBehaviour
{
    public GameObject enemyPf;
    public int enemyCount;
    public int waveNumber = 1;
    public GameObject powerUp;
    

    // Start is called before the first frame update
    void Start()
    {
        SpawnWaves(waveNumber);
        SpawnPowerUp();

    }

    // Update is called once per frame
    void Update()
    {
        enemyCount = FindObjectsOfType<Enemy>().Length;
        if(enemyCount == 0)
        {
            waveNumber++;
            SpawnWaves(waveNumber);
            SpawnPowerUp();
            
        }
    }

    Vector3 SpawnEnemyPos()
    {
       Vector3 spawnPos = new Vector3(Random.Range(-10, 10), 0, Random.Range(10, - 10));
        return spawnPos;
    }

    void SpawnWaves(int spawnNumber)
    {
        for (int i = 0; i < spawnNumber; i++)

        {
            Instantiate(enemyPf, SpawnEnemyPos(), enemyPf.transform.rotation);
        }
}

    void SpawnPowerUp()
    {
        Instantiate(powerUp, SpawnEnemyPos(), powerUp.transform.rotation);
    }
}
 

Rotate 

Enemy

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

public class Rotate : MonoBehaviour
{
    public float speed = 3;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(Vector3.up * speed);
    }
}
 

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

public class Enemy : MonoBehaviour
{
    private Rigidbody enemyRb;
    private GameObject playerRb;
    public float speed = 7;

    // Start is called before the first frame update
    void Start()
    {
        enemyRb = GetComponent<Rigidbody>();
        playerRb = GameObject.Find("Player");
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerPos = (playerRb.transform.position - transform.position).normalized;
        enemyRb.AddForce(playerPos * speed);

        if (transform.position.y < -10)
            Destroy(gameObject);
    }
}
 

Focal Point

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

public class Enemy : MonoBehaviour
{
    private Rigidbody enemyRb;
    private GameObject playerRb;
    public float speed = 7;

    // Start is called before the first frame update
    void Start()
    {
        enemyRb = GetComponent<Rigidbody>();
        playerRb = GameObject.Find("Player");
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 playerPos = (playerRb.transform.position - transform.position).normalized;
        enemyRb.AddForce(playerPos * speed);

        if (transform.position.y < -10)
            Destroy(gameObject);
    }
}
 

bottom of page