- Home /
Question by
dangerndle · Jun 03, 2018 at 01:53 AM ·
playerjump
My player is able to double jump, any way to make it a single jump? (C#)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class player_move_prototype : MonoBehaviour {
public int playerSpeed = 10;
private bool facingRight = false;
public int playerJumpPower = 1250;
private float moveX;
public bool isGrounded;
// Update is called once per frame
void Update () {
PlayerMove ();
PlayerRaycast ();
}
void PlayerMove() {
moveX = Input.GetAxis("Horizontal");
if (Input.GetButtonDown ("Jump") && isGrounded == true) {
Jump ();
}
if (moveX < 0.0f && facingRight == false) {
FlipPlayer ();
}else if (moveX > 0.0f && facingRight == true) {
FlipPlayer ();
}
gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2 (moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
}
void Jump() {
GetComponent<Rigidbody2D>().AddForce (Vector2.up * playerJumpPower);
isGrounded = false;
}
void FlipPlayer () {
facingRight = !facingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
}
void OnCollisionEnter2D (Collision2D col){
isGrounded = true;
}
void PlayerRaycast () {
RaycastHit2D hit = Physics2D.Raycast (transform.position, Vector2.down);
if (hit != null && hit.collider != null && hit.distance < 0.9f && hit.collider.tag == "enemy") {
GetComponent<Rigidbody2D> ().AddForce (Vector2.up * 900);
Debug.Log ("Squished enemy");
Destroy (hit.collider.gameObject, 0.1f);
}
if (hit.distance < 0.9f && hit.collider.tag != "enemy") {
isGrounded = true;
Debug.Log ("Player has touched " + hit.collider.tag);
}
}
}
Comment