Y
Y
yungbalykva2021-09-13 22:02:57
C++ / C#
yungbalykva, 2021-09-13 22:02:57

Stopped jumping sprite in Unity, what should I do?

I started making a 2D platformer for android, I know c# at an intermediate level, I sort of figured out the unit. I did everything according to the video: " https://www.youtube.com/watch?v=sX0hhZT9wDg&list=P... " At first everything worked well, the script was executed, the sprite jumped. As it was intended. After a while, I decide to move the platform a little lower along with the main character, and then I notice that the sprite has stopped jumping, to nothing. The jump key bind changed, the script is correct. Help what to do?? Here is the actual script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class chelik : MonoBehaviour
{
    [SerializeField] float speed = 3f;
    [SerializeField] int lives = 5;
    [SerializeField] float jumpForce = 1.4f;
    bool Ground = false;

    Rigidbody2D rb;
    SpriteRenderer sprite;


    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Horizontal"))
            Run();
        if (Ground && Input.GetButton("Jump"))
            Jump();
    }

        void Awake()
    {
        rb = GetComponent<Rigidbody2D>();
        sprite = GetComponentInChildren<SpriteRenderer>();
    }

        void Run()
    {
        Vector3 dir = transform.right * Input.GetAxis("Horizontal");
        transform.position = Vector3.MoveTowards(transform.position, transform.position + dir, speed * Time.deltaTime);

        sprite.flipX = dir.x < 0.0f;
    }

        void Jump()
    {
        rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
    }

        void Grounded()
    {
        Collider2D[] collider = Physics2D.OverlapCircleAll(transform.position, 1f);
        Ground = collider.Length > 1;
    }

        void FixedUpdate()
    {
        Grounded();
    }


}


Maybe the problem is in some Unity settings??

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Kuzmenko, 2021-09-13
@morex972

I don’t really understand rigidbody, I’m more into characterController well, if you moved and stopped jumping the character, it’s possible that the character sprite is stuck in the platform and doesn’t jump

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question