So Im trying to code a script that will get me to transform position of my character when float value is not equal to null.
This is my activator script
void OnTriggerEnter2D(Collider2D col) {
if (col.GetComponent<PlayerMovement> ()) {
//Percentage = x / 187.5
//Common = 8, Rare = 5, VeryRare = 2
float c = 100 / 187.5f;
float r = 5 / 187.5f;
float vr = 2 / 187.5f;
float p = Random.Range (0.0f, 100.0f);
if (p < c * 100) {
if (battle != null)
battle.enterBattle (Rarity.Common);
} else if (p < r * 100) {
if (battle != null)
battle.enterBattle (Rarity.Rare);
} else if (p < vr * 100) {
if (battle != null)
battle.enterBattle (Rarity.VeryRare);
}
}
}
this is my battle scipt
public class Battle : MonoBehaviour {
Vector2 currentPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
currentPos = transform.position;
}
public void enterBattle(Rarity rarity) {
currentPos = new Vector2 (2, 22);
}
public void leaveBattle() {
currentPos = new Vector2 (1, 1);
}
}
Does anyone know how to code this to work, or give examples that I can understand how to make it work. Thanks.
The first thing to do is begin liberally sprinkling Debug.Log();
statements in your code at strategic points, partly to see if code is even running (first problem), and then to see what various values are that result in decisions.
ALSO, with variable names like p, c, r, and vr, it is going to be basically impossible for your code to communicate to anybody, including your future self, what it means. This means humans cannot reason about it. That’s a problem. Fix that problem. Make meaningful variable names. It’s the LEAST you can do for yourself.
The small variables stand for common rare and very rare I set common to an extremely high number so I can get instant battle for testing
And I know for fact that battle.enterbattle does not work because it’s not hooked to anything, that’s the part I dont know how to code
So definitely start back with the API documentation:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
The col
you are checking is what the moving object (where this script is located) hits.
Do you expect something moving to hit the player and then that something to start battle? That is certainly possible but it seems backwards from the way Unity games are usually laid out.
This is also an odd statement which I will attribute to a simple typo, as a float value cannot be equal to null, as they are value types and not reference types. I assume you mean “less than” some other value.
Again, use Debug.Log() to get the code running in the first place (vis-a-vis the specific requirements outlined in the documentation above), then once it is running, if it does not work, start outputting the values used by the various variables in the decisions.
Keep in mind nobody here can do that. It’s your project, your code, your design idea.
Then name it such or else your code will rapidly become inscrutible. Trust me.
The top script snippet certainly SEEMS to hook into it.
It might be best if you back up and review some tutorials as far as how to do state transitions between “moving around” and “being in battle” for instance, or whatever existing game is most like your game idea, watch some tutorials along that line in order to get a feel for how to break the problem space down reasonably.
The moving around activator is not the problem since it’s a box collider on an object like I said I don’t know how to code in battle.enterbattle…
I’m not clear what your problem is, and your code with the nonsense variable names is painful to try to read. Are you seeing enterBattle actually being called? Is battle ever not null?
I fixed one part but no triggers are being activated it may be because its always null like it says in the monodev