public class Player : MonoBehaviour
{
[SerializeField]
private float speed = 10.0f;
private float horizontalInput;
[SerializeField]
private float verticalInput;
[SerializeField]
private float jumpHeight = 5.0f;
private Rigidbody2D myrigidbody2D;
private bool isgrounded;
float Ypos;
float YposVerify;
// Use this for initialization
void Start()
{
myrigidbody2D = GetComponent<Rigidbody2D>();
isgrounded = false;
}
// Update is called once per frame
void Update()
{
Movement();
if (Input.GetKeyDown(KeyCode.Z) && isgrounded == true)
{
//transform.position += new Vector3(transform.position.x, transform.position.y + 20.0f,0);
myrigidbody2D.velocity = new Vector2(0, speed * jumpHeight);
}
}
private void Movement()
{
transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);
horizontalInput = Input.GetAxis("Horizontal");
}
//make sure u replace "floor" with your gameobject name.on which player is standing
void OnCollisionEnter(Collision theCollision)
{
if (theCollision.gameObject.name == "Ground")
{
isgrounded = true;
}
}
//consider when character is jumping .. it will exit collision.
void OnCollisionExit(Collision theCollision)
{
if (theCollision.gameObject.name == "Ground")
{
isgrounded = false;
}
}
}