Question by
unity_SxtF7NirPcouuA · Mar 12, 2020 at 10:19 AM ·
unity 2d
My player stopped jumping!
My character stopped jumping when I change my grid to a layer that isn't set as a walkable surface.
my C# script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterController : MonoBehaviour{
private Rigidbody2D rb;
public float speed;
public float jumpForce;
private float moveInput;
private bool isGrounded;
public Transform feetPos;
public float checkRadius;
public LayerMask whatIsGround;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
if(moveInput > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
else if(moveInput < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
if(isGrounded == true && Input.GetKeyDown(KeyCode.Space))
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if(jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
jumpTimeCounter -= Time.deltaTime;
}
else
{
isJumping = false;
}
}
}
}
Comment
Answer by tormentoarmagedoom · Mar 12, 2020 at 10:33 AM
Hello.
You must DEBUG your code, and check the satses of your variables, and see why the code is not doing what you expect. we can not check the states of all variables, only you can.
Your answer
Follow this Question
Related Questions
How to make fast a coroutine 1 Answer
Compiler error 0 Answers
Jittery background when implementing parallax effect 1 Answer
Problem getting components in loaded scene 0 Answers
Instantiating A group of platform and moving them down 0 Answers