Golem Power Up – Speed Boost

To explore UI scripts and game object interaction, I created a power up that the player can pick up and use as a speed boost.

The setup was fairly simple for the power up. Two short scripts, one to animate the object and one for player interaction.

Animate.cs


The animate script had three functions: Bounce up and down, rotate, and disappear on collision.

 void Update() {
    Bounce();
    Rotate();
  }

To bounce up and down, I just translated the Y position over Mathf.Sin() over time.

void Bounce() {
    // floating
    float X = transform.position.x;
    float Y = Mathf.Sin(Time.time * BounceSpeed) * BounceHeight + YOffset;
    float Z = transform.position.z;
    transform.position = new Vector3(X, Y, Z);
  }

The rotation was just a simple rotation over time.

void Rotate() {
    // rotating
    transform.RotateAround(transform.position, Vector3.up, RotationSpeed * Time.deltaTime);
  }

To make the power up disappear for a set amount of time, I used a coroutine to enable/ disable the object. In the OnTriggerEnter function, of the game object is a player and the power up is active, the power up is disabled and activity set to false and the coroutine is started. When the coroutine is finished, the power up is reactivated and enabled.

private void OnTriggerEnter(Collider other) {
    if (other.gameObject.tag == "Player") {
      if (Active) {
        MR.enabled = false;
        Active = false;
        StartCoroutine(Dissappear());
      }
    }
  }
  IEnumerator Dissappear() {
    yield return new WaitForSeconds(PowerUpRespawnTime);
    MR.enabled = true;
    Active = true;
  }

PowerUp.cs


The pick up script is simple. Using the OnTriggerEnter function, if the game object tag is “Player” and there is a valid player behavior script, it will add the power up to the player. Initially, I did not have it reference the player behavior from the OnTriggerEnter function and quickly realized that that would only work in a single player scenario.

public class PowerUp : MonoBehaviour {
  private void OnTriggerEnter(Collider other) {
    PlayerBehavior playerBehavior = other.GetComponent<PlayerBehavior>();
    if (playerBehavior != null) {
      PowerUpCollide(playerBehavior);
    }
  }
  private void PowerUpCollide(PlayerBehavior playerBehavior) {
    playerBehavior.AddPowerUp();
  }
}

Working Speed Boost Power Up


Project Links

You can follow my project on GitHub here: https://github.com/Scottin3d/GolemPlayground