Answer the question
In order to leave comments, you need to log in
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:
Staircase:
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question