- Home /
How to successfully apply force calling a function from another script
Hi, I have a problem that is making me crazy... I have a 2D platform kind off game, where the user can only move on X and Y axis, well the thing is, Im moving the player with Physx applying forces from a C# Scrip called PlayerMovement, I have two functions called Subir() and Bajar() (means up and down)
public void Subir()
{
if (!Atascado)
{
rigidbody.AddRelativeForce((Vector3.up * acelVert * Time.deltaTime) * 1.5f, ForceMode.Impulse);
print("subiendo a "+GetComponent<CharacterProperties>().Name);
if (detenido)
{
acelVert = 10;
}
if (!detenido)
{
acelVert += 50;
}
up = true;
}
}
public void Bajar()
{
if (!Atascado)
{
rigidbody.AddRelativeForce((Vector3.up * -acelVert * Time.deltaTime) * 1.5f, ForceMode.Impulse);
print("bajando a " + GetComponent<CharacterProperties>().Name);
if (detenido)
{
acelVert = 10;
}
if (!detenido)
{
acelVert += 50;
}
down = true;
}
}
now from the very PlayerMovement Script im calling those functions just like
if (Input.GetKey(KeyCode.DownArrow))
{
Bajar();
}
if (Input.GetKey(KeyCode.UpArrow))
{
Subir();
}
and it works Great! but from other side I have a C# script called GameBasics from there Im drawning some GUITexture buttons and catching their inputs with the mouse position like this:
if (Input.GetMouseButtonDown(0))
{
if (turboBtn.HitTest(Input.mousePosition))
{
_pMovement.LanzaTurbo();
}
if (switchPlayerBtn.HitTest(Input.mousePosition))
{
SwitchPlayer();
}
}
if (Input.GetMouseButton(0))
{
if (upArrowBtn.HitTest(Input.mousePosition))
{
_jugador.GetComponent<PlayerMovement>().Subir();
}
if (dwnArrowBtn.HitTest(Input.mousePosition))
{
_jugador.GetComponent<PlayerMovement>().Bajar();
}
}
Now here's the problem the first to "LanzarTurbo()" and "SwitchPlayer()" are working fine when I click the corresponding buttons on the screen, but when I click on the other two, apparently the Subir() and Bajar() functions are being called but no force is been applied to my character. I know Subir() and Bajar() works cause I printed some output on the console and I can see from both ways, with the Keyboard Arrows and with the GUITexture buttons, but the second ones arent really applying the force to my object, any ideas why this is happening? or any suggestions to solve that? as you can see on the PlayerMovement Subir() and Bajar() functions I have a print() function that prints some output, output that can be seen through both ways, but the rigidbody.AddRelativeForce() is not reflecting any changes on my character with the function being called from other script.
Actually if you pay close attention to the code you will see that im calling the functions from Get$$anonymous$$ouseButton() and not from Get$$anonymous$$ouseButtonDown(), anyway thanks :D
Answer by Noztradamuz · May 31, 2013 at 03:36 PM
I Managed to do this by creating a bool variable on the PlayerMovement script and calling the function Subir() and Bajar() if the bool variable is true, and I switch those variables on the GetKey and by making the bool variables public I was able to access them from the GameBasics script and apply the force correctly, there is something curious, for some reason if I call the functions Subir() and Bajar() from somewhere besides the Input.GetKey() function they do nothing. But fortunately I managed to do this correctly, thanks for the quick answer.
Your answer
Follow this Question
Related Questions
Inputing an Equation 0 Answers
Using var as a placeholder for velocity... why? 2 Answers
Parenting Preventing Player Rotation 0 Answers
Multiple Cars not working 1 Answer