【问题标题】:All bodies have rigidbody 2d and boxcollider2d and there's no collision所有物体都有刚体 2d 和 boxcollider2d 并且没有碰撞
【发布时间】:2016-03-26 00:41:47
【问题描述】:

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

我在 youtube 上关注来自 unity 的 Tutorial Playlist,我使用 GameManager 和 BoardManager 来创建我的地图。
这是我在墙上预制件上的检查员

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

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 左为 false),则会发生碰撞,但盒子会移动,这与我的意图相反。当我检查Is Kinetic时,没有再次发生碰撞。

【问题讨论】:

  • 什么版本的统一(精确)?
  • 好吧,不是那个问题。其次,物理有什么不寻常的地方,你能贴出墙上的脚本吗?
  • 哦,忘了说 - 墙上的脚本只是空的,我可以把它完全删除。这是教程墙脚本的其余部分,我最终没有实施,因为它不适合我的游戏。
  • 你改变了Physics2D,有不同的碰撞层吗?
  • 按照教程,我创建了一个名为 BlockingLayer 的新层(以及一个新的排序层,但不是这样),播放器和墙预制件都分层为 BlockingLayer。除此之外,我没有与 Physics2D 互动。

标签: c# unity3d


【解决方案1】:

编辑-您的播放器已检查“运动学”!我相信这是你的问题!

由统一文档“如果启用 isKinematic,力、碰撞或关节将不再影响刚体。” -http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

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

2.) 检查您的碰撞矩阵。我注意到“玩家”有一个图层“BlockingLayer”。 (您可能想要更改它)但是如果在 Edit->Project Settings->Physics2D 中未选中 'BlockingLayer' x 'BlockingLayer' 复选框,则可以解释没有碰撞。

【讨论】:

  • 所有墙壁都有 Rigidbody2D 并且 IsKinematic 被选中,这是肯定的。这也与层无关。图层交互设置我没碰过,就算我把图层全部改成默认还是不行
  • 墙无论如何都不需要刚体,它们只需要盒子碰撞器。为什么这会得到支持?
  • 那你有什么建议?如果他们不需要刚体,我会摆脱它,但这并不能改变它不起作用的事实......
【解决方案2】:

已解决 - 玩家不应该是运动的。应该取消选中 IsKinematic,Rigidbody2D 中的重力应该设置为 0。谢谢大家。现在将继续讨论下一个碰撞错误:)

【讨论】:

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