【发布时间】:2017-06-07 02:39:50
【问题描述】:
大家好,我在制作 2D 平台游戏时遇到了困难。到目前为止,我已经为玩家提供了可以跳跃和行走的矩形景观。现在我想开始添加一些小山。想象一个三角形,左侧代表玩家可以向上行走的小山。我已经制作了景观,并从我的其他路径复制了 Box Collider 2D。我想出了一种方法来旋转对撞机,使它像三角形的斜率一样以 45 度角上升。出于某种原因,当我走进三角形时,玩家并没有开始向上走。我必须跳到山上,他降落在一个看起来像看不见的矩形上!我使对撞机的厚度为 0.25,所以我知道我将整个对撞机放在我想要的地方。知道为什么吗?我真的没有任何代码,因为我所做的一切几乎都在场景视图中。谢谢大家!
这是处于播放模式的玩家,被某种力量站在山顶上:
这是碰撞器在场景视图中的样子。我已经移动了对撞机,它仍然做同样的事情:
这是我的移动代码:
public class Player : MonoBehaviour {
public float maxSpeed = 3;
public float speed = 50f;
public float jumpPower = 150f;
public bool grounded;
private Rigidbody2D rb2d;
private Animator anim;
void Start () {
rb2d = gameObject.GetComponent<Rigidbody2D>();
anim = gameObject.GetComponent<Animator>();
}
void Update () {
anim.SetBool("Grounded",grounded);
anim.SetFloat("Speed", Mathf.Abs(rb2d.velocity.x));
if (Input.GetAxis("Horizontal") < -0.1)
{
transform.localScale = new Vector3(-1, 1, 1);
}
if (Input.GetAxis("Horizontal") > 0.1)
{
transform.localScale = new Vector3(1, 1, 1);
}
if (Input.GetButtonDown("Jump") && grounded)
{
rb2d.AddForce(Vector2.up * jumpPower);
}
}
void FixedUpdate() {
Vector3 easeVelocity = rb2d.velocity;
easeVelocity.y = rb2d.velocity.y;
easeVelocity.z = 0.0f;
easeVelocity.x *= 0.75f;
float h = Input.GetAxis("Horizontal");
//Fake friction / Easing the x speed of our player
if (grounded)
{
rb2d.velocity = easeVelocity;
}
//Moving the Player
rb2d.AddForce((Vector2.right * speed) * h);
//Limiting the Speed of the Player
if (rb2d.velocity.x > maxSpeed)
{
rb2d.velocity = new Vector2(maxSpeed, rb2d.velocity.y);
}
if (rb2d.velocity.x < -maxSpeed)
{
rb2d.velocity = new Vector2(-maxSpeed, rb2d.velocity.y);
}
}
}
地面材质应用于此景观,就像其他矩形景观一样。对不起,我是新人,但再次感谢大家!
【问题讨论】:
-
看起来你在那个小山精灵对象中有另一个碰撞器。你确定只有一个吗?您能否分享该资产的检查员视图图像?
-
是的,我认为这是唯一的一个。实际上放弃了它并重新开始。我拿了一个我知道可以工作的矩形平台,然后将它旋转了大约 30 度。他可以很好地走上去。我注意到,如果我在山前停下然后走路,他上山的速度会慢一点,我假设这是一个物理问题。下坡时他走得很快,并在滑下时做“跳跃”动画。同样,可能与物理学有关。我可能不得不观看其他用户在这里提供给我的视频,因为我认为这在项目中很常见。
-
@JosepValls 没有编辑他的原始帖子,而是选择发布新帖子,至少操作者应该删除旧帖子。
-
@Alox 刚刚看到了。 OP 是否使用了不同的用户名?