Alright, got it, there are multiple issues with this, but you did a great start :3
It was easy to pick up, so good job on that :)
You've intended for the Orbs to be picked up, adding combo to a visible gauge as they dissapear, your first major flaw lies in, Probably, not having the Player or Actor, the "other" gameobject as it may have a Rigidbody. For Collision / Triggering to occur, you'll need to give the player a rigidBody.
The second possible flaw is not having "Trigger" Checked on the Orb objects, this doesn't matter as much, because they'll dissapear immediately, but the difference is that with:
- Trigger Unchecked, Player has an Actual Collision with the object and it'll slow the player down.
- Trigger Checked, Player will phase through the object and it won't slow him down.
Trigger or not, your object will have collision messages, but it does define whether it makes other objects feel the impact or whether it's just a ghost ;O
See: http://unity3d.com/learn/tutorials/modules/beginner/physics/colliders-as-triggers
On to the next flaw, now, your collision is working, however, things are still messed up. There is no gauge, because the Orb object that holds it gets destroyed. ;O
Your current setup, possibly:
![alt text][1]
This is your code reworked as it was, just so you have that as a nice improvement example:
using UnityEngine;
using System.Collections;
public class ComboGauge : MonoBehaviour
{
// Public Members
public float m_MaxCombo = 100;
public float m_CurCombo = 0;
// Holder Variables
private float ComboBarLength;
void Start()
{
ComboBarLength = Screen.width / 2;
}
void OnGUI()
{
GUI.Box(new Rect(10, 10, ComboBarLength, 20), m_CurCombo + "/" + m_MaxCombo);
}
public void AdjustCurrentCombo(float adj)
{
// Store the new value of m_CurCombo + adj in m_CurCombo.
// But if necessary clamp it to be greater or equal to 0 and less than or equal to m_MaxCombo
m_CurCombo = Mathf.Clamp(m_CurCombo + adj, 0, m_MaxCombo);
// m_MaxCombo never changes yet, so no need to do this.
//if (m_MaxCombo < 1)
// m_MaxCombo = 1;
// In case you will in the future, use this:
// m_MaxCombo = Math.Max(1, newvalue); (uses greater value of two values, in this case forcing the new value to be always greater or equal to 1)
// Update ComboBarLength, before, m_CurCombo was an int and m_MaxCombo was cast to a float, you probably want both to be floats so you can have decimal values out of the equation :)
// ComboBarLength = (Screen.width / 2) * (m_CurCombo / (float)m_MaxCombo);
// New variant, note that the parenthesis are unnecessary, but they do look the part :3
ComboBarLength = (Screen.width / 2) * (m_CurCombo / m_MaxCombo);
// Remove this and the above comments if you feel like it, less polution, yay :3
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "orb")
{
// Use our method to add 10 to the current combo and update the combobar :)
AdjustCurrentCombo(10);
Destroy(gameObject);
}
}
}
So, yay, pretty code, but still, the orb that holds the Gauge gets destroyed, so this won't work, let's add another script that holds the Gauge stuff, and then redo this script to turn it into a PFB_Orb script. :)
Ok, sweet, got it to work.
For your scene, aside from a camera and a directional light to see shit, you'll want:
- an Empty GameObject with the Gauge_Handler script on it.
- a Player object with a collider and rigidbody, tagged with "Player"
- an X amount of Orbs in the scene to pickup with a collider with Trigger checked and the PFB_Orb script on them.
See here:
![alt text][2]
Here's the code for the 2 scripts, the ComboGauge script is no longer existent :3
using UnityEngine;
using System.Collections;
public class PFB_Orb : MonoBehaviour {
void OnTriggerEnter(Collider other)
{
// The Player picks up the orbs, it's more logical to ask the other object whether it's a Player, because after all, You're the orb xD
if (other.gameObject.tag == "Player")
{
// Use our method over at Gauge_Handler to add 10 to the current combo and update the combobar :)
// Thanks to the Singleton magic, we can easily access the Gauge_Handler from any script.
// WARNING: Singleton, as the name implies, can have only one active object on it.
// If you have multiple Gauge_Handlers, the script won't know which instance to get.
// So, there can be only one Gauge_Handler, which is logical, because you don't want multiple overlapping guages for now.
Gauge_Handler.Instance.AdjustCurrentCombo(10);
Destroy(gameObject);
}
}
}
Above is the PFB_Orb script, below is the Gauge_Handler script. :)
using UnityEngine;
using System.Collections;
public class Gauge_Handler : MonoBehaviour {
// Apply Singleton
public static Gauge_Handler Instance;
void Awake() { Instance = this; }
// Public Members
public float m_MaxCombo = 100;
public float m_CurCombo = 0;
// Holder Variables
private float ComboBarLength;
void Start()
{
ComboBarLength = Screen.width / 2;
}
void OnGUI()
{
GUI.Box(new Rect(10, 10, ComboBarLength, 20), m_CurCombo + "/" + m_MaxCombo);
}
public void AdjustCurrentCombo(float adj)
{
// Store the new value of m_CurCombo + adj in m_CurCombo.
// But if necessary clamp it to be greater or equal to 0 and less than or equal to m_MaxCombo
m_CurCombo = Mathf.Clamp(m_CurCombo + adj, 0, m_MaxCombo);
// m_MaxCombo never changes yet, so no need to do this.
//if (m_MaxCombo < 1)
// m_MaxCombo = 1;
// In case you will in the future, use this:
// m_MaxCombo = Math.Max(1, newvalue); (uses greater value of two values, in this case forcing the new value to be always greater or equal to 1)
// Update ComboBarLength, before, m_CurCombo was an int and m_MaxCombo was cast to a float, you probably want both to be floats so you can have decimal values out of the equation :)
// ComboBarLength = (Screen.width / 2) * (m_CurCombo / (float)m_MaxCombo);
// New variant, note that the parenthesis are unnecessary, but they do look the part :3
ComboBarLength = (Screen.width / 2) * (m_CurCombo / m_MaxCombo);
// Remove this and the above comments if you feel like it, less polution, yay :3
}
}
There you go, just piece it together now, and it's all nice and working ;)
If you have any more questions, or need more details, feel free to ask, if not, please accept this answer and have a very nice day! :D
[1]: /storage/temp/35533-example.png
[2]: /storage/temp/35534-example2.png
↧