gpt4 book ai didi

c# - 所有物体都有 rigidbody 2d 和 boxcollider2d 并且没有碰撞

转载 作者:行者123 更新时间:2023-11-30 23:32:12 26 4
gpt4 key购买 nike

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

我正在关注这个 Tutorial Playlist来自 unity on youtube,我使用 GameManager 和 BoardManager 来创建我的 map 。
这是我在墙上预制件上的检查员
Inspector On Walls
这是我的 Player 预制件上的检查器
Inspector On 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
}
}

这是我在游戏中的问题示例:

Ingame photo

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

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

最佳答案

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

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

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

2.) 检查你的碰撞矩阵。我注意到“播放器”有一层“BlockingLayer”。 (您可能想要更改它)但是如果在 Edit->Project Settings->Physics2D 中未选中 'BlockingLayer' x 'BlockingLayer' 复选框,那么这可以解释没有碰撞的原因。

关于c# - 所有物体都有 rigidbody 2d 和 boxcollider2d 并且没有碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34370294/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com