Hi, I am trying to port over a function that I had in JS which worked fine, but now I have put it in C# it no longer works. Please could someone help me?
Here is my original JS code.
Here is the function starting:
function Start () {
playerOneJumpTime (); //calling the function to allow jumping - comment out to disable jumps
playerOneStats = GameObject.Find("GameObjectPlayerOne").GetComponent(ScriptPlayerOneStats); // this sets the variable "playerOneStats" to the correct script
}
Here is the function:
function playerOneJumpTime() // This function controls the players jump - makes the player wait to be able to jump again
{
while (true)
{
if (Input.GetKeyDown (GameObject.Find("GameObjectPlayerOne").GetComponent(ScriptPlayerOneStats).playerOneControlJump))
{
rigidbody.velocity = Vector3(0,GameObject.Find("GameObjectPlayerOne").GetComponent(ScriptPlayerOneStats).playerOneJumpPower,0);
yield WaitForSeconds(playerOneStats.playerOneJumpRate);
}
else
{
yield;
}
}
}
Now here is the C# version
Here is the function starting:
void Start()
{
rigidbody.freezeRotation = true; // Prevent the player from falling over due to physics
playerOneJumpTime();
}
Here is the function:
IEnumerator playerOneJumpTime() // This function controls the players jump - makes the player wait to be able to jump again
{
while (true)
{
float jumpPower = playerPhysics.playerOneJumpPower;
float jumpDelay = playerPhysics.playerOneJumpRate;
if (Input.GetKeyDown(playerControls.playerOneControlJump))
{
rigidbody.velocity = new Vector3(0, jumpPower, 0);
yield return new WaitForSeconds(jumpDelay);
}
else
{
yield return null;
}
}
}
Any help would be greatly appreciated