所有物体都具有刚体2d和boxcollider2d,并且没有碰撞

朱奥拉·古赛

我正在学习统一性,并且正在尝试从XNA在Unity中重新创建自己的游戏。

我正在youtube上按照本教程播放列表进行操作,并且已经使用GameManager和BoardManager来创建地图。
这是我的墙壁预制件上的检查员这是我的播放器预制件上的检查员
墙壁检查员

播放器上的检查器

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
    }
}

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

游戏中照片

如您所见,玩家可以简单地进出墙壁,就像他们没有盒子对撞机一样。

在写这篇文章时,我注意到,如果我给墙壁预制件提供Rigidbody2D(Is Kinetic左为假),则会发生碰撞,但盒子会移动,这与我的意图相反。当我检查时Is Kinetic,不再有冲突。

Nothin'ButNet应用程序

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

通过统一的文档“如果启用isKinematic,部队,碰撞或接缝不会影响刚体了。 ” - http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

1.)确保“墙”具有Rigidbody2D和Rigidbody2D.isKinematic已选中

2.)检查您的碰撞矩阵。我注意到“玩家”具有一层“ BlockingLayer”。(您可能想要更改它),但是如果在“编辑”->“项目设置”->“ Physics2D”中未选中“ BlockingLayer” x“ BlockingLayer”复选框,则可以解释缺少冲突。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有 boxcollider 的 Unity 2D C# hitbox

来自分类Dev

没有摩擦的 Box2d 物体在碰撞时以某种方式获得速度

来自分类Dev

Unity中的BoxCollider2D界限

来自分类Dev

没有图块XNA的2D游戏中的碰撞检测

来自分类Dev

没有图块XNA的2D游戏中的碰撞检测

来自分类Dev

具有ggplot和R的2D普通PDF

来自分类Dev

Unity BoxCollider2D旋转与GameObject旋转不同

来自分类Dev

如何在所有元素都相同的python中创建2D列表?

来自分类Dev

Ruby:从2D数组中收集具有匹配元素的所有数组

来自分类Dev

具有所有按行排列的Tensorflow1 concat 2D张量

来自分类Dev

如何解决与固定物体的2D碰撞?

来自分类Dev

2D运动物体碰撞

来自分类Dev

忽略Cocos2D中具有相同碰撞组的对象之间的碰撞

来自分类Dev

如何在具有碰撞检测功能的Unity中制作自定义2D多边形?

来自分类Dev

在将GameObject与Unity 3D拖动时,如何使用GameObject与BoxCollider 2D碰撞时如何检测碰撞

来自分类Dev

是否可以将 BoxCollider2D 转换为 PolygonCollider2D?

来自分类Dev

如何在没有STL的情况下从2d阵列中删除所有未填充的插槽?

来自分类Dev

互斥的2D阵列的所有组合

来自分类Dev

消除椭圆外的所有2D数据点

来自分类Dev

查找并返回给定2D数组的所有循环

来自分类Dev

2D数组,所有值相邻

来自分类Dev

进行2D阵列中的所有可能组合

来自分类Dev

2D数组但具有索引-Java

来自分类Dev

具有相同值的Python 2D数组

来自分类Dev

具有不同列数的C ++ 2D数组

来自分类Dev

具有2D阵列的C内存管理

来自分类Dev

具有数据类型的2D NumpyArray

来自分类Dev

具有2D数组的MPI_Allgather

来自分类Dev

具有功能的2D阵列scanf

Related 相关文章

热门标签

归档