So I have a game that I am developing.
I want to program it so that once one of those buttons are pressed if you move the joystick, the button/limb that is pressed will move with the joystick. My issue is that I need to create a
If Buttons A-D (Left Hand, Right Hand, Left Foot, Right Foot) are pressed Then
Apply x amount of force with the direction of the Joystick
My issue is I do not know how to reference buttons A-D in a seperate script from the buttons themselves. I am working inside of Unity’s Mobile Input SingleStick Control.
Bump. Still need help on this.
Can you provide the script you currently have?
Whether or not each button is pressed is set to be boolean variables. So if two are pressed at the same time you can control both, that goes all the way to four. What I’m trying to see is if I could set up:
If LeftHandButton Pressed {
LeftHandBool or LHP = true
} else {
LHP = false;
}
public class LimbsController : MonoBehaviour {
public float moveForce = 5;
public Rigidbody2D leftHand;
public Rigidbody2D leftFoot;
public Rigidbody2D rightHand;
public Rigidbody2D rightFoot;
public Button btnLH;
public Button btnRH;
public Button btnLF;
public Button btnRF;
// I decided that having whether or not the button was pressed as a boolean would be easier to call from the Joystick script
public bool LHP; //Left Hand Pressed
public bool RHP; //Right Hand Pressed
public bool LFP; //Left Foot Pressed
public bool RFP; //Right Foot Pressed
// Use this for initialization
void Start () {
leftHand = GetComponent<Rigidbody2D>();//Declaring each body part to be applied by physics
leftFoot = GetComponent<Rigidbody2D>();
rightHand = GetComponent<Rigidbody2D>();
rightFoot = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
btnLF.onClick.AddListener (FeedbackLF);
btnLH.onClick.AddListener (FeedbackLH);
btnRF.onClick.AddListener (FeedbackRF);
btnRH.onClick.AddListener (FeedbackRH);
}
void FeedbackLF()
{
LFP = true;
}
void FeedbackLH()
{
LHP = true;
}
void FeedbackRF()
{
RFP = true;
}
void FeedbackRH()
{
RHP = true;
}
}
You may need to look into OnPointerDown and OnPointerUp functions for all of your buttons, OnClick only runs if the object is clicked so these options may be more suitable.
Is OnPointer supported by Mobile?
This could doesn’t look right. If these are public, you should assign them in the inspector.
Right now, you’re asking the script to assign the rigidbody2d component (on the same GO as this script) to each lefthand, left foot, etc…
I don’t think you’re getting the right references
// Use this for initialization
void Start () {
leftHand = GetComponent<Rigidbody2D>();//Declaring each body part to be applied by physics
leftFoot = GetComponent<Rigidbody2D>();
rightHand = GetComponent<Rigidbody2D>();
rightFoot = GetComponent<Rigidbody2D>();
}
You could also put a script on each hand/foot and use the button’s ClickEvent to target a method on (those) scripts to do your addition of force/movement…
I do have them assigned in the inspector:
Cool, if you assigned them there yourself, you may as well remove the code from Start().
Right now, you’re fetching the Rigidbody2D in Start() (whatever’s on the object). When the game is running and you click on those variables in the inspector – do they highlight the correct components/game objects?
I would like to add your FixedUpdate is not correct. If you plan to addListeners through code, you should only be doing it once. In start is usually a good spot for that.
Also, yes, the OnPointerDown/Up work on mobile.
oh wow… either didn’t read enough or fell asleep at the wheel. Totally agree with the FixedUpdate() comment/fix.