- Home /
Help! How can I get my power up script to work?
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;
}
}
First, you can use code tags to show your code. It is not readable now.
Second, you can use
Destroy(other.gameObject);
//ins$$anonymous$$d of below. You already have a reference to "SuperSpeed" object.
//Destroy(GameObject.FindWithTag ("SuperSpeed"));
your trigger method might not be working, you can test it by putting a debug inside OnTriggerEnter, so you can see at the console that trigger event is working.
Debug.Log("Trigger Entered");
Answer by bubzy · Nov 27, 2014 at 09:57 PM
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);
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Powerup Script to make player invincible? 1 Answer
How can i fix this error? 2 Answers
NullReferenceException: I think unity bug 3 Answers
Animation for Powerup Won't Work? 1 Answer