Hi all
Ive got what seems to be a quite simple question. For some reason i cannot make my character jump from the ground after i started to implement a logic that would prevent him from doublejumping.
Ive implemented my characters movement like this:
public class PlayerController : MonoBehaviour {
private Vector3 walkSpeed;
private Vector3 jumpHeight;
private float distToGround;
// Use this for initialization
void Start () {
walkSpeed = new Vector3(4, 0, 0);
jumpHeight = new Vector3(0, 12, 0);
distToGround = collider.bounds.extents.y;
}
// Raycast under the player to detect if he is grounded
private bool IsGrounded()
{
return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
}
// Update is called once per frame
void Update () {
}
void FixedUpdate () {
// Player Movement
if (Input.GetAxisRaw("Horizontal") == 1)
{
rigidbody.MovePosition(rigidbody.position + walkSpeed * Time.deltaTime);
}
else if (Input.GetAxisRaw("Horizontal") == -1)
{
rigidbody.MovePosition(rigidbody.position + walkSpeed * -1 * Time.deltaTime);
}
// Player Jumping
if (IsGrounded() && Input.GetButtonDown("Jump"))
{
rigidbody.AddForce(jumpHeight, ForceMode.Impulse);
}
}
}
For some reason (checking with debug.log) my character never clears the 2 if statements checking if he is grounded and if the jump button is pressed.
The weird thing is. If i test on the if statements 1 at a time theyre working just fine.
Can anybody help me?
↧