Hi, im a beginner in gam dev. Is there something wrong with my code?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
Animator animator;
Rigidbody2D rb2d;
SpriteRenderer spriterenderer;
bool isgrounded;
[SerializeField]
Transform groundcheck;
// Start is called before the first frame update
void Start()
{
animator = GetComponent();
rb2d = GetComponent();
spriterenderer = GetComponent();
}
// Update is called once per frame
private void FixedUpdate()
{
if (Physics2D.Linecast(transform.position,groundcheck.position, 1 << LayerMask.NameToLayer(“ground”)))
{
isgrounded = true;
}
else
{
isgrounded = false;
}
if (Input.GetKey(“d”) || Input.GetKey (“right”))
{
rb2d.velocity = new Vector2(7, rb2d.velocity.y);
if (isgrounded)
animator.Play(“char_run”);
spriterenderer.flipX = false;
}
else if (Input.GetKey(“a”)|| Input.GetKey(“left”))
{
rb2d.velocity = new Vector2(-7, rb2d.velocity.y);
if (isgrounded)
animator.Play(“char_run”);
spriterenderer.flipX = true;
}
else
{
if (isgrounded)
animator.Play(“char_idle”);
rb2d.velocity = new Vector2(0, rb2d.velocity.y);
}
if (Input.GetKey(“space”) && isgrounded)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, 3);
animator.Play(“char_jump”);
}
}
}
Here is the problem, everytime i remove all the “if(isgrounded)” from the inputs, my character can walk left and right perfectly fine but jumping is another issue. It keeps replaying the animation for jumping when i hit spacebar or hold onto spacebar.
but with this code above i mentioned, is the one that stops me from moving my character. whenever i hit left or right. it wont move at all but only plays the animation left and right and the jump button wont even work for me.