我正在为一名自由职业者制作2D平台游戏,而且我必须做一个音速技师,我必须使角色在不跌倒的情况下经过一个完整的循环,我意识到必须首先旋转角色贴合他经历了循环,但第二部分是我被困住的地方。
因此,基本上,如何使角色不跌倒地运行循环。
private void OnCollisionEnter2D(Collision2D coll)
{
Vector3 collRotation = coll.transform.rotation.eulerAngles;
if (coll.gameObject.tag == "Ground")
{
//in this part i rotate the player as he runs through the loop
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, collRotation.z);
//this is the part that i am stuck, trying to figure out how to make the character stay in the loop without falling, i tried collision detection,
//i tried raycasting but nothing seems to work
if (IsGrounded)
{
GetComponent<Rigidbody2D>().AddForce(Vector2.down, ForceMode2D.Impulse);
}
}
}
重制和类似操作最常用的技巧是提高玩家角色进入时的速度。
// Feel free to adjust this to whatever works for your project.
const float minimumAttachSpeed = 2f;
// This should be your characters current movement speed
float currentSpeed;
// You need a Rigidbody in this example, or you can just disable
// any custom gravity solution you may have created
Rigidbody2D rb;
如果角色的速度超过了最小附件速度,则可以允许他们以该速度遵循预定义的路径。
bool LoopAttachmentCheck()
{
return minimumAttachSpeed <= currentSpeed;
}
现在,您可以检查自己的移动速度是否足够快!我假设在此示例中,您正在使用Rigidbody2D ...
(此检查仅应在您进入或当前处于循环中时运行)
void Update()
{
if( LoopAttachmentCheck() )
{
// We enable this to prevent the character from falling
rb.isKinematic = true;
// Here you write the code that allows the character to move
// in a circle, e.g. via a bezier curve, at the currentSpeed.
}
else
{
rb.isKinematic = false;
}
}
实施实际的旋转行为取决于您。如果我是您,那么执行此操作的一种好方法(假设它是一个完美的圆)将从圆心开始使用RotateAround。
如果形状更复杂,则可以使用航路点进行移动,并以自己的速度遍历它们。
当您无法保持速度时,您的角色将掉落(例如,如果玩家决定停止跑步),Rigibody2D将变得运动。
希望这可以帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句