Jumping disables my horizontal movement.
Hey there, I've recently run into a little problem, whenever I jump my Horizontal movement just stops.. I have no idea when it started happening or what could be causing it, I'm probably just overlooking something extremely obvious or have something somewhere in an If statement that it shouldn't be.. anyways if someone could help or send me in a direction, I have look for solutions and nothing has worked so far..
Here's my entire script, the movement is at the top of the Update, When I add another script that runs AllInOne.GetComponent(); inside the Start void and then save it while my game is running it suddenly works, but as soon as I stop running the game and run it again, I get my ussie of jumping and moving again.. I would also like to point out that while I am falling from the jump I can move perfectly fine, it's only while I am moving upwards.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AllInOne : MonoBehaviour
{
public float inputVertical;
public Animator playerAnim;
public Animator cameraAnim;
public float speed;
public float moveInput;
public float jumpTimeCounter;
public float jumpTime;
public float jumpForce;
private bool isJumping;
public bool isGrounded;
public bool isClimbing;
public Transform feetPos;
public LayerMask whatIsGround;
public LayerMask whatIsLadder;
public Rigidbody2D rb;
public float checkRadius;
public float GetVerticalSpeed() => rb.velocity.y;
public float distance;
//Particles
public GameObject deathEffect;
public GameObject landEffect;
//Allow partciles
public bool spawnDust;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//Movement
//Horizontal Movement AKA Walking
moveInput = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
playerAnim.SetFloat("Speed", Mathf.Abs(moveInput));
if (moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if (moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
//Jump
if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
Instantiate(deathEffect, feetPos.position, Quaternion.identity);
}
//jump higher but not too high or too long thing
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
//Ladder
RaycastHit2D hitInfo = Physics2D.Raycast(transform.position, Vector2.up, distance, whatIsLadder);
if (hitInfo.collider != null)
{
playerAnim.SetBool("Jump", false);
playerAnim.SetBool("IsFalling", false);
//set gravity to slow slow for when falling down
rb.gravityScale = 0.01f;
//when key make gravity 0 and let me move up and down
}
if (Input.GetKeyDown(KeyCode.W))
{
isClimbing = true;
}
else
{
isClimbing = false;
rb.gravityScale = 5;
playerAnim.SetBool("Ninja", false);
}
if (isClimbing == true)
{
inputVertical = Input.GetAxis("Vertical");
rb.velocity = new Vector2(rb.velocity.x, inputVertical * speed);
rb.gravityScale = 0;
}
//Animations
RaycastHit2D ladInfo = Physics2D.Raycast(transform.position, Vector2.up, distance, whatIsLadder);
if (ladInfo.collider != null)
{
if (GetVerticalSpeed() > 0.1)
{
playerAnim.SetBool("Ninja", true);
}
if (GetVerticalSpeed() < 0.1)
{
playerAnim.SetBool("Ninja", true);
}
}
//animation stuff
if (isGrounded == true)
{
playerAnim.SetBool("Jump", false);
playerAnim.SetBool("IsFalling", false);
playerAnim.SetBool("Ninja", false);
}
else
{
if (GetVerticalSpeed() > 0.1 && isClimbing == false)
{
playerAnim.SetBool("Jump", true);
playerAnim.SetBool("IsFalling", false);
cameraAnim.SetBool("can", false);
}
else if (GetVerticalSpeed() < 0.1 && isClimbing == false)
{
playerAnim.SetBool("IsFalling", true);
playerAnim.SetBool("Jump", false);
cameraAnim.SetBool("can", true);
}
else if (GetVerticalSpeed() == 0 && isClimbing == false)
{
playerAnim.SetBool("Jump", false);
playerAnim.SetBool("IsFalling", false);
cameraAnim.SetBool("can", false);
}
}
//particle
if (isGrounded == true)
{
if (spawnDust == true)
{
cameraAnim.SetBool("shakywaky", true);
Instantiate(landEffect, feetPos.position, Quaternion.identity);
spawnDust = false;
}
}
else
{
spawnDust = true;
cameraAnim.SetBool("shakywaky", false);
}
}
}
Your answer
Follow this Question
Related Questions
Character landing 0 Answers
[C#] Can't Control Movement While Jumping 0 Answers
Why is root motion reduced once I apply the animation? 0 Answers
Player flying into oblivion. 2 Answers