I am making a retro-like Metroidvania game and am building a test level to test all my power ups, and I am trying to make one that lets the player run faster this is my script so far NOTE: Needs to be in JavaScript : #pragma strict
var Speed : float = 12;
function Update ()
{
if (Input.GetKey (KeyCode.Space))
{
transform.Translate(Vector3(0,Speed,0) * Time.deltaTime);
}
if (Input.GetKey (KeyCode.A))
{transform.localScale.x = 1;
transform.Translate(Vector3(-Speed,0,0) * Time.deltaTime);
}
if (Input.GetKey (KeyCode.D))
{
transform.localScale.x = 1;
transform.Translate(Vector3(Speed,0,0) * Time.deltaTime);
}
}
function OnTriggerEnter(other : Collider)
{
if(other.tag == "SuperSpeed");
{
Destroy(GameObject.FindWithTag ("SuperSpeed"));
Speed = Speed + 8;
}
}
yuck, i would write a seperate script for the powerup and have an “effect” on it
(im not going to apologise for using c# because imo its better :P)
like
using UnityEngine;
using System.Collections;
public class bonusPickup : MonoBehaviour {
// Use this for initialization
public float speedBonus;
public float healthBonus;
public float jumpBonus;
void Start () {
//this can be a speedBoost although i would usually make prefabs for each one instead of coding
//that way you can make something like smallBoost = 4 bigBoost = 8 or whatever
speedBonus = 8f;
}
// Update is called once per frame
void Update () {
}
}
and then on the player
using UnityEngine;
using System.Collections;
public class playerScript : MonoBehaviour {
// Use this for initialization
private float speed = 2f;
private float health = 100f;
private float jumpHeight = 1f;
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "powerup")
{
bonusPickup tempPowerup;
tempPowerup = collision.gameObject.GetComponent<bonusPickup>();
speed += tempPowerup.speedBonus;
health += tempPowerup.healthBonus;
jumpHeight += tempPowerup.jumpBonus;
Destroy(collision.gameObject);
}
}
}