- Home /
Rigidbody Controller Jump
I am making a platformer type game and am using a rigidbody controller that I made. The jumping system works when I directly implement jumping within the update, after: moveTo *= MOVE_SPEED
and before adding force to the rigidbody. How can I implement jumping in a separate function (mainly so that I can call the function from a jump-boost).
EDIT: I posted the new code, the jumping works, but only when the jump() is called after the regular addforce(moveTo). The jump() function is being called, but when it is called before addforce(moveTo), and from an external script it does not make the player jump.
Current Code:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
public class PlayerController : MonoBehaviour
{
public JumpState jumpState;
const float MOVE_SPEED = 5f;
public bool IsGrounded
{
get
{
return Physics.Raycast(transform.position, -Vector3.up, collider.bounds.extents.y + 0.01f);
}
}
public static PlayerController Get
{
get
{
return FindObjectOfType<PlayerController>();
}
}
void Start()
{
rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}
void Update()
{
rigidbody.velocity = new Vector3 (0f, rigidbody.velocity.y, 0f);
Vector3 moveTo = new Vector3 (Input.GetAxis ("Horizontal"),
0, Input.GetAxis ("Vertical"));
moveTo = transform.TransformDirection (moveTo);
moveTo *= MOVE_SPEED;
rigidbody.AddForce (moveTo, ForceMode.Impulse);
if(IsGrounded)
jumpState = JumpState.Grounded;
if(Input.GetButtonDown("Jump") && jumpState != JumpState.DoubleJumped)
{
Jump();
jumpState = (jumpState == JumpState.Grounded) ? JumpState.Jumped : JumpState.DoubleJumped;
}
}
public void Jump(float jumpHeight = 5f)
{
// This gets called.
Debug.Log ("Jump!");
Vector3 jumpPos = transform.up * jumpHeight;
rigidbody.AddForce (jumpPos , ForceMode.Impulse);
}
public enum JumpState
{
Grounded,
Jumped,
DoubleJumped
}
}
If your looking for the equivalent of Get$$anonymous$$ey it is GetButton not GetButtonDown.
What is not working? Is the jump method executing but you're not jumping? Are you sure the method is executing?
Yes, it is still a problem if I change to fixed update.