Hi, I am at the moment coding the movement script for my player object, but I’ve encountered a problem where the player jumps like twice, making him jump really high, I think it’s because I make the player jump if he is grounded and the left joystick goes above 0.8, so sometimes it inputs twice before leaving the ground, is there any workaround for that? here’s my code:
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour {
Rigidbody2D body2D;
Transform myTransform;
bool facingRight = true;
public Transform graphicsObject;
public Transform swordObject;
float direction;
public float walkVelocity;
public float lerpVelocity;
public float jumpVelocity;
bool isJumping;
void Start () {
body2D = GetComponent<Rigidbody2D>();
myTransform = GetComponent<Transform>();
}
bool isGrounded () {
RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, -Vector2.up, 1.05f);
foreach (RaycastHit2D hit in hits) {
if (hit.transform != myTransform) {
return true;
}
}
return false;
}
void Update () {
direction = Mathf.Lerp(direction,Input.GetAxis("HorizontalLeft"), lerpVelocity * Time.deltaTime);
direction = Mathf.Clamp(direction, -1f, 1f);
if (isGrounded()) {
if (Input.GetButtonDown("Jump") || Input.GetAxis("VerticalLeft") >= 0.8f) {
body2D.velocity += new Vector2(0f, jumpVelocity);
}
}
body2D.velocity = new Vector2(direction * walkVelocity, rigidbody2D.velocity.y);
if (swordObject.localRotation.z <= 0.7f && swordObject.localRotation.z >= -0.7f) {
graphicsObject.localScale = new Vector3 (1f, graphicsObject.localScale.y, graphicsObject.localScale.z);
}
else {
graphicsObject.localScale = new Vector3 (-1f, graphicsObject.localScale.y, graphicsObject.localScale.z);
}
}
void OnGUI () {
GUILayout.Label(facingRight.ToString() + " " + swordObject.localRotation.z.ToString("F2") + " isGrounded " + isGrounded());
}
}
Thanks in advance, btw, the inputs are set for gamepads, that’s the reason why I want to use left joystick to jump and give the option to also use a button.