Персонаж падает вниз - Unity

написал простенький код для передвижения персонажа в unity. Вот он, если убрать воду и ненужный код.

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

public class PlayerTranslate : MonoBehaviour
{
    public Text p1;
    public Text p2;
    public int hp = 100;
    public float speed;
    public int PlayerNumber;
    public GameObject[] weapons;
    public GameObject wepUp;
    public GameObject wepNorm;
    public Sprite personUp;
    public Sprite personNorm;
    public Vector3 PersonScale;
    public float horizontalSpeed = 999f;
    float speedX;
    public float versticalimpulse;
    Rigidbody2D rb;
    // Start is called before the first frame update
    void Start()
    {
        PersonScale = gameObject.transform.localScale;
        rb = GetComponent<Rigidbody2D>();
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.RightShift))
        {
            print("jump!");
            rb.velocity = Vector2.up * versticalimpulse;
        }
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        if (PlayerNumber == 2)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                gameObject.transform.localScale = new Vector3(-PersonScale.x, PersonScale.y, PersonScale.z);
                speedX = -horizontalSpeed;
                gameObject.GetComponent<SpriteRenderer>().sprite = personNorm;
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = personNorm;
                gameObject.transform.localScale = PersonScale;
                speedX = horizontalSpeed;
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                foreach (GameObject wep in weapons)
                {
                    wep.transform.position = wepUp.transform.position;
                    wep.transform.rotation = wepUp.transform.rotation;
                }
                gameObject.GetComponent<SpriteRenderer>().sprite = personUp;
            }
            else
            {
                foreach (GameObject wep2 in weapons)
                {
                    wep2.transform.position = wepNorm.transform.position;
                    wep2.transform.rotation = wepNorm.transform.rotation;
                }
                gameObject.GetComponent<SpriteRenderer>().sprite = personNorm;

            }
    
            rb.velocity = new Vector2(speedX * speed, rb.velocity.y);
            speedX = 0;
        }
}

Проблема заключается в том, что когда персонаж падает с какой-либо высоты, он с большой скоростью проваливается в пол(если он изначально находился на какой-либо поверхности, то никуда не проваливался и мог спокойно ходить.) Как я могу это исправить?


Ответы (0 шт):