Box碰撞2D Unity,侧向碰撞不起作用

问题描述 投票:-3回答:1

所以我在Unity中发生框冲突的问题是,当我的角色从顶部或底部与框碰撞时,它可以正常工作并停下来,但是当它从左侧或右侧移向它时,它不会停下来,这不是我想要发生的事情。如果有人可以告诉我我在做什么错,那太好了。 (Unity 2019.2.12f1)

角色的盒冲突

Box collsion of the character

平台的盒式碰撞

Box collsion of the platform

 public class playermovement : MonoBehaviour
{
    public float spelerspeed = 8.0f;
    public float top = 276.0f;
    public float bottom = -270.0f;
    public bool isgrounded = false;
    public bool iswall = false;
    public int var = 0;
    public float jumpheight = 11500f;
    public float Djumpheight = 11500f;
    private Rigidbody2D rigidbody;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {

        userinput();
    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.collider.tag == "ground")
        {
            isgrounded = true;
            var = 0;
        }
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.collider.tag == "ground")
        {
            isgrounded = false;
            var = 1;
        }

    }void userinput()
    {
        if (Input.GetKeyDown(KeyCode.Space) && var == 1)
        {
            Rigidbody2D rb2d = gameObject.GetComponent<Rigidbody2D>();
            rb2d.velocity = new Vector2(rb2d.velocity.x, 0);
            rb2d.AddForce(Vector2.up, ForceMode2D.Impulse);
            var = 2;

          gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, Djumpheight), ForceMode2D.Impulse);

        }
        if (Input.GetKeyDown(KeyCode.Space) && isgrounded == true)
        {
            var = 1;
            gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpheight), ForceMode2D.Impulse);
        }
        if (transform.localPosition.x >= top)
        {
            transform.localPosition = new Vector3(-269, -83,0);
        }
        if (transform.localPosition.x <= bottom)
        {
            transform.localPosition = new Vector3(275, -83, 0);
        }
        if (Input.GetKey(KeyCode.A)|| Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.D) == false) 
        {
            if (Input.GetKey(KeyCode.D))
            {

            }
            else
            {
                transform.localRotation = Quaternion.Euler(0, 180, 0);
            }

        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)&& Input.GetKey(KeyCode.A) == false)
        {
            if (Input.GetKey(KeyCode.A))
            {

            }
            else
            {
                transform.localRotation = Quaternion.Euler(0, 0, 0);
            }
        }

       Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
       transform.localPosition += move * spelerspeed * Time.deltaTime;


    }

}
c# unity3d rigid-bodies
1个回答
0
投票

找到解决方案,必须更改刚体的移动方式:

我更改了:

Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.localPosition += move * spelerspeed * Time.deltaTime;

进入:

public float runSpeed = 40f;
horizontalMove = Input.GetAxisRaw("Horizontal")* Time.deltaTime
Vector3 targetVelocity = new Vector2(horizontalMove * runSpeed; * 10f, m_Rigidbody2D.velocity.y);
m_Rigidbody2D.velocity = Vector3.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_MovementSmoothing);
© www.soinside.com 2019 - 2024. All rights reserved.