Unity 2D how to simulate height using triggers and a variable
Hello
I would like to know if anyone can come up with a solution to this;
I'm trying to produce a 2D game, but want to simulate height. I have a number variable that is assigned to a ground object that has a trigger attached to it.
A player game object is supposed to check whether the player is in one of these ground height triggers, and if the player is in one of these triggers, it assigns the height number of the ground object to the player's height number.
Essentially If the player enters a ground with a level of 1 it's ground level is 1 if it enters a ground with a level of 2 it's ground level is 2 etc.
Answer by CobbledGames · Jan 23, 2019 at 11:13 AM
The ground variable is set up in it's own script, the player's variable is set and the check for the ground triggers is set in another. I might need a little more guidance for the method of getting the collider and Var. I'll see what I can do though.
No it seems I don't have this figured out. $$anonymous$$ay I ask how to to do this: "you would be able to access the collider.gameObject.getcomponent of that script and access the var and just assigned it in your script."
that completly depends of your code, can you share it so i can guide you?
Ground level assigning script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class GroundLvlAssign : $$anonymous$$onoBehaviour { public float SetGroundLvl = 1f; public Collider2D m_GroundCollider;
}
Checking Script:
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class PlayerStatistics : $$anonymous$$onoBehaviour {
public float m_PlayerGroundLvl = 1; //What level of ground object is on.
GroundLvlAssign m_GroundLvlAssign; //GroundlvlNumberScript
public Collider2D m_CenterOfBalance;
void OnTriggerEnter2D (Collider2D ground)
{
}
}
Where SetGroundLvl is the Height level of the ground object, GroundCollider is it's collider and CenterOfBalance is the collider on the player object that is used for checking the ground level. Some of the 2nd script is missing because it isn't relevant to the question.
Answer by xxmariofer · Jan 22, 2019 at 11:10 PM
Well, the solution is in your own question, just ontriggerenter2d method for getting the event, you said you have a number variable assigned to the ground, how is that var assigned? is inside the script? you would be able to access the collider.gameObject.getcomponent of that script and acces the var and just assigned it in your script.