Y
Y
YakutD2018-12-12 16:57:24
2D
YakutD, 2018-12-12 16:57:24

Why is the character being pushed down the stairs?

Making it possible to climb vertical ladders in a 2D game. As a result, the character climbs the stairs, but he is constantly pushed somewhere from this very ladder. If, at the moment of climbing, you press the character control keys left and right (to resist being pushed out), then the character simply "pounds", shakes as if there is some kind of obstacle in front of them. What's my mistake? I am attaching screenshots of the Persian inspectors and stairs, as well as the climbing code.
Character:
5c11136bc9bf9331791609.png5c1113743415b673881045.png
Staircase:
5c11137fce47c006161816.png
Code:

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

public class Climbing : MonoBehaviour {
    private PlayerController player;

    public float distance;
    public LayerMask whatisLadder;
    private bool isClimbing;
   
    // Use this for initialization
    void Start () {
        player = GetComponent<PlayerController>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float Horizontal = Input.GetAxisRaw("Horizontal");
        float Vertical = Input.GetAxisRaw("Vertical");

        RaycastHit2D hitinfo = Physics2D.Raycast(transform.position, Vector2.up, distance, whatisLadder);

        if (hitinfo.collider != null)
        {
            player.canjump = false;
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                isClimbing = true;
            }
            else if (Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.LeftArrow))
            {
                isClimbing = false;
            }
        }
        else
        {
            player.canjump = true;
        }

        if (isClimbing && hitinfo.collider != null)
        {
            player.rb.velocity = new Vector2(player.rb.position.x, Vertical * player.speed);
            player.rb.gravityScale = 0;
        }
        else
        {
            player.rb.gravityScale = 3;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
YakutD, 2018-12-12
@YakutD

If FixedUpdate is replaced with just Update, then the problem disappears.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question