【问题标题】:Why is my player getting stuck in walls Unity 2D为什么我的播放器卡在墙上 Unity 2D
【发布时间】:2019-08-18 02:41:14
【问题描述】:

我正在制作一个统一 2d 的游戏,当我的玩家撞到墙上时,他被卡住了,根本无法移动。这是一个视频:

VIDEO

我尝试使用复合对撞机,摩擦力为 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);
    }
}

【问题讨论】:

  • 你的视频链接好像坏了;你能用正确的版本编辑链接吗?

标签: c# unity3d gameobject


【解决方案1】:

在您的案例中,Player 对象包含一个刚体组件。所以,最好使用Rigidbody的一些移动方法,比如MovePosition(),而不是直接通过transform.position改变GameObject的位置

【讨论】:

  • 谢谢我没有使用 MovePosition() 但我使用了不同的方法谢谢
【解决方案2】:

感谢@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);
    }

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多