- Home /
Jump Function C# hassle!
Well, I am currently working on a 2D game like Mario!
But, I am currently stuck with the jump function!
The game object has a rigidbody attached!
The camera view is on XZ Plane!!
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public float speed = 1.0f;
public float jumpSpeed = 10000.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
Physics.gravity = new Vector3(0f, 0f, -0.5f);
transform.Translate(Input.GetAxis("Horizontal") * speed * Time.deltaTime,0,0);
if(Input.GetKeyDown(KeyCode.W))
{
rigidbody.AddRelativeForce(0,0,jumpSpeed*Time.deltaTime);
}
}
}
The Problem is I can't get the player to jump well!!
I need the player to jump in the Z Axis!!
But the behaviour of jump are different in two cases!!
When the player is idle, the jump is exaclty as needed!
When player is running, the jump is not working well!
It works kinda like its not jumping at all!!
P.S. I know I havent made the jump function with isGrounded variable based! But, that's the least of my worries for now! It's easy to fix grounded state!
Help me to make the both cases work exactly as they are needed!
I know how to make it work with the character controller!!
But just wanted to know how to script my own!!