Hi,
I'm wanting to create a 2D platform game with physics based character control. I have tried the Character Controller component which does work really well for the most part but my character is spherical in shape and I would like to take advantage of physics when rolling down curved slopes etc. I have started coding and I'm running into a problem with jumping. I want my character to jump in a perfect arc unless the player changes direction in the air but the basic jump method from the reference doesn't do that. I'm sure others have seen it but basically the ball slows abruptly as it jumps up then speeds up as it falls back down.
Here is my code,
public class PhysicsControl : MonoBehaviour
{
#region Public Variables
public float speed = 10.0f;
public float jumpSpeed = 10;
#endregion
#region Private Variables
bool isGrounded = false;
Rigidbody myRigidbody;
#endregion
// Use this for initialization
void Start()
{
myRigidbody = rigidbody;
}
// Update is called once per frame
void FixedUpdate()
{
//calculate movement
Vector3 moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
myRigidbody.AddForce(moveDirection * speed);
if (isGrounded)
{
Jump();
}
isGrounded = false;
}
void OnCollisionStay()
{
isGrounded = true;
}
void Jump()
{
if (Input.GetButton("Jump"))
{
myRigidbody.velocity = Vector3.up * jumpSpeed;
}
}
}
Any help would be great.
Thanks
Dan
p.s. I am also open to suggestions on how to use the character controller along with rigidbody if there is a way to seamlessly switch between the two as needed. I did try that option though with no success.