本文介绍了所有物体都具有刚体2d和boxcollider2d,并且没有碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习统一性,我正在尝试通过XNA在Unity中重新创建自己的游戏.

I'm learning unity, and I'm trying to recreate a game of my in Unity from XNA.

我正在YouTube上关注此教程播放列表,曾使用GameManager和BoardManager来创建我的地图.
这是我的墙面预制件检查员

这是我的Player预制板上的检查器

I'm following this Tutorial Playlist from unity on youtube, and I've used the GameManager and BoardManager to create my map.
This is my inspector on the wall prefabs

And this is the inspector on my Player prefab

PlayerMovement脚本的代码

using UnityEngine;

namespace Assets.Scripts
{
    public enum Directions
    {
        Back,
        Left,
        Front,
        Right,
        Idle = -1
    }

    public class PlayerMovement : MonoBehaviour
    {

        #region Public Members

        public float speed;

        #endregion

        #region Constants

        private const float DECAY_FACTOR = 0.85f;
        private const float SPEED_FACTOR = 20000f;

        #endregion

        #region Private Members

        private Rigidbody2D rb2D;
        private Vector2 velocity;
        private Animator animator;

        #endregion

        #region Game Loop Methods

        private void Awake()
        {
            animator = GetComponent<Animator>();
            rb2D = GetComponent<Rigidbody2D>();
        }

        private void Update()
        {
            float vertical = Input.GetAxisRaw("Vertical");
            float horizontal = Input.GetAxisRaw("Horizontal");
            UpdateVelocity(vertical, horizontal);
            UpdateAnimation();
            UpdateMovment();
        }

        #endregion

        #region Animation Methods

        private void UpdateAnimation()
        {
            Directions direction;

            if (velocity.y > 0)
                direction = Directions.Back;
            else if (velocity.y < 0)
                direction = Directions.Front;
            else if (velocity.x > 0)
                direction = Directions.Right;
            else if (velocity.x < 0)
                direction = Directions.Left;
            else
                direction = Directions.Idle;

            SetDirection(direction);
        }

        private void SetDirection(Directions value)
        {
            animator.SetInteger("Direction", (int)value);
        }

        #endregion

        #region Movement Methods

        private void UpdateMovment()
        {
            Debug.Log(string.Format("HOR - {0} : VER - {1} : DIR - {2}", velocity.x, velocity.y, animator.GetInteger("Direction")));
            transform.Translate(velocity.x, velocity.y, 0f, transform);
            ApplySpeedDecay();
        }

        private void UpdateVelocity(float vertical, float horizontal)
        {
            if (vertical != 0)
                velocity.y += Mathf.Abs(speed) / SPEED_FACTOR;
            if (horizontal != 0)
                velocity.x += Mathf.Abs(speed) / SPEED_FACTOR;
        }

        private void ApplySpeedDecay()
        {
            // Apply speed decay
            velocity.x *= DECAY_FACTOR;
            velocity.y *= DECAY_FACTOR;

            // Zerofy tiny velocities
            const float EPSILON = 0.01f;

            if (Mathf.Abs(velocity.x) < EPSILON)
                velocity.x = 0;
            if (Mathf.Abs(velocity.y) < EPSILON)
                velocity.y = 0;
        }

        #endregion
    }
}

这是我在游戏中遇到问题的一个例子:

Here's an example of my problem ingame:

如您所见,玩家可以像没有箱子对撞机一样简单地进出墙壁.

As you can see, the player can simply move in and out of walls as if they don't have box collider.

在撰写本文时,我注意到,如果我给墙壁预制件提供Rigidbody2D(Is Kinetic保留为false),则会发生碰撞,但盒子会移动,这与我的意图相反.当我检查Is Kinetic时,不再有碰撞.

While writing this post, I've noticed that if I give the wall prefabs a Rigidbody2D (with Is Kinetic left false) there's collision but the boxes move, which is the opposite of my intent. When I check Is Kinetic, there's not collision again.

推荐答案

编辑-您的播放器已选中"iskinematic"!我相信这是您的问题!

Edit-Your player has 'iskinematic' checked! I believe this is your problem!

通过统一文档"如果启用了isKinematic,则力,碰撞或关节将不再影响刚体."- http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

By the unity docs "If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore."-http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

2.)检查碰撞矩阵.我注意到玩家"具有一层"BlockingLayer". (您可能想要更改它),但是如果在编辑"->项目设置"->"Physics2D"中未选中"BlockingLayer" x"BlockingLayer"复选框,那么这将说明缺少冲突.

2.) Check your collision matrix. I noticed that 'player' has a layer 'BlockingLayer'. (You'll probably want to change that) But if in Edit->Project Settings->Physics2D the 'BlockingLayer' x 'BlockingLayer' checkbox is unchecked, then that would explain the lack of collisions.

这篇关于所有物体都具有刚体2d和boxcollider2d,并且没有碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:33