Почему мой плеер застревает в стенах Unity 2D

Я делаю игру в Unity 2D, и когда мой игрок входит в стену, он застревает и вообще не может двигаться. Вот видео:

ВИДЕО

Я пытался использовать составной коллайдер, физический материал с трением на 0.

Вот мой сценарий движения:

public class PlayerMovement : MonoBehaviour
{
    Vector3 pos;
    float speed = 2.0f;
    private Animator animator;
    void Start()
    {
        pos = transform.position;
        animator = gameObject.GetComponent<Animator>();
    }

    void FixedUpdate()
    {
        if (Input.GetKey(KeyCode.W) && transform.position == pos)
        {        // Up
            animator.SetInteger("isWalking", 1);
            pos += Vector3.up;
        }
        if (Input.GetKey(KeyCode.S) && transform.position == pos)
        {        // Down
            animator.SetInteger("isWalking", 2);
            pos += Vector3.down;
        }
        if (Input.GetKey(KeyCode.D) && transform.position == pos)
        {        // Right
            animator.SetInteger("isWalking", 3);
            pos += Vector3.right;
        }
        if (Input.GetKey(KeyCode.A) && transform.position == pos)
        {        // Left
            animator.SetInteger("isWalking", 4);
            pos += Vector3.left;
        }
        if (Input.anyKey == false)
            animator.SetInteger("isWalking", 0);
        transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
    }
}

person Omar    schedule 18.08.2019    source источник
comment
Ваша ссылка на видео кажется неработающей; Можете ли вы отредактировать ссылку с правильной версией?   -  person Kaynn    schedule 18.08.2019


Ответы (2)


Объект Player в вашем случае содержит компонент Rigidbody. Таким образом, было бы лучше использовать некоторые методы перемещения Rigidbody, такие как MovePosition(), вместо того, чтобы изменять положение GameObject напрямую через transform.position

person Nitro557    schedule 18.08.2019
comment
Спасибо, я не использовал MovePosition(), но я использовал другой метод, спасибо. - person Omar; 18.08.2019

Благодаря @Nitro557 у меня появилась новая идея вместо того, чтобы просто телепортировать игрока, я использовал совершенно другой метод перемещения игрока, вот сценарий:


    public float runSpeed = 2.0f;
    private Rigidbody2D body;
    private Animator animator;
    private float horizontal;
    private float vertical;
    private float moveLimiter = 0.7f;
    void Start()
    {
        body = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
    }
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");
        vertical = Input.GetAxisRaw("Vertical");
        if(Input.GetKeyDown(KeyCode.LeftShift))
        {
            runSpeed += 0.5f;
        }
    }
    private void FixedUpdate()
    {
        if (horizontal != 0 && vertical != 0)
        {
            horizontal *= moveLimiter;
            vertical *= moveLimiter;
        }
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
        // Up
        if (Input.GetKey(KeyCode.W))
            animator.SetInteger("isWalking", 1);
        // Down
        if (Input.GetKey(KeyCode.S))
            animator.SetInteger("isWalking", 2);
        // Right
        if (Input.GetKey(KeyCode.D))
            animator.SetInteger("isWalking", 3);
        // Left
        if (Input.GetKey(KeyCode.A))
            animator.SetInteger("isWalking", 4);
        if (Input.anyKeyDown == false)
            animator.SetInteger("isWalking", 0);
        body.velocity = new Vector2(horizontal * runSpeed, vertical * runSpeed);
    }

person Omar    schedule 18.08.2019