Hello Everyone,
My problem has probably already been discussed here before. I found several similar problems, but no solution (if any) did not help in my case.
I’m creating my first endless runner game. There are two problems:
- Very often my character randomly jumps too high in the air after the jump, as in the video shown:
Please help me, what is missing in the script of my character (under the second problem) that this jump appears. Is an attribute missing in the script, or some tag in the hierarchy in Unity?
- What should be changed in touch control (maybe in this player control script) so that the character doesn’t jump while holding down the button, but requires that you press the jump button (touch the sceen) again?
My character script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerControl : MonoBehaviour
{
Animator anim;
Rigidbody2D rb;
[SerializeField]
float jumpForce = 500f;
float upOrDown;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (GameControl.gameStopped != true)
{
upOrDown = CrossPlatformInputManager.GetAxisRaw("Vertical");
if (upOrDown > 0 && rb.velocity.y == 0)
rb.AddForce(Vector2.up * jumpForce);
if (upOrDown < 0 && rb.velocity.y == 0)
anim.SetBool("isDown", true);
else
anim.SetBool("isDown", false);
}
if (GameControl.gameStopped != true)
{
upOrDown = CrossPlatformInputManager.GetAxisRaw("Vertical");
if (upOrDown > 0 && Mathf.Abs(rb.velocity.y) == 0)
rb.AddForce(Vector2.up * jumpForce);
if (upOrDown > 0)
anim.SetBool("isJump", true);
else
anim.SetBool("isJump", false);
}
}
}
Thank you in advance for your help!