使播放器转动中空气时放慢速度
我想进行类似于超级马里奥兄弟的跳跃,这意味着您可以移动空气中间空气,但是如果您朝着相反的方向移动,您会放慢脚步。
例如,如果您朝正确的方向跳跃,但向左中间空中移动,则可以向左移动,但非常缓慢。我尝试通过添加一个IF子句来检查玩家是否向左移动,然后播放速度的一半,但由于某种原因不起作用。
这是我的代码:
private Rigidbody rb;
[SerializeField] private float jumpForce;
[SerializeField] private float playerSpeed;
private float distToGround = 0.0051904f;
public bool isGrounded = false;
private bool _jump;
private float _horizontal;
private bool _movingLeft;
private bool _movingRight;
private bool _turnedLeft;
private bool _turnedRight;
private BoxCollider playerFeet;
private void Awake()
{
rb = gameObject.GetComponent<Rigidbody>();
}
private void Start()
{
}
private void Update()
{
GetInputs();
}
private void FixedUpdate()
{
//PlayerJump
if (_jump && _turnedRight == true)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
rb.AddForce(new Vector3(0, jumpForce), ForceMode.Impulse);
_jump = false;
}
else if (_jump && _turnedLeft == true)
{
transform.rotation = Quaternion.Euler(0, -180, 0);
rb.AddForce(new Vector3(0, jumpForce), ForceMode.Impulse);
_jump = false;
}
//RotatePlayerDependingWhereHeMoves
if(_movingLeft == true && isGrounded)
{
gameObject.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
_movingLeft = false;
}
if (_movingRight == true && isGrounded)
{
gameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
_movingRight = false;
}
//MovePlayer
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, _horizontal * -playerSpeed);
//ChecksIfGrounded
GroundCheck();
}
void GetInputs()
{
_horizontal = Input.GetAxisRaw(Tags.HORIZONTAL_AXIS);
if(isGrounded && Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Player pressed Jump");
_jump = true;
}
if(_horizontal == -1)
{
_movingLeft = true;
}
else if(_horizontal == 1)
{
_movingRight = true;
}
if(transform.rotation.y == 0)
{
_turnedRight = true;
}
else if(transform.rotation.y == -180)
{
_turnedLeft = true;
}
}
void GroundCheck()
{
if(Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
I wanted to do a jump similar to Super Mario Bros meaning you can move mid air but if you move in the opposite direction that you started your jump in you will slow down.
For example if you jump in the right direction but move to the left mid-air you can move to the left but very slowly. I tried doing it by adding a if clause that checks if the player is moving to the left and then half the player speed but for some reason that didn't work.
Here's my code:
private Rigidbody rb;
[SerializeField] private float jumpForce;
[SerializeField] private float playerSpeed;
private float distToGround = 0.0051904f;
public bool isGrounded = false;
private bool _jump;
private float _horizontal;
private bool _movingLeft;
private bool _movingRight;
private bool _turnedLeft;
private bool _turnedRight;
private BoxCollider playerFeet;
private void Awake()
{
rb = gameObject.GetComponent<Rigidbody>();
}
private void Start()
{
}
private void Update()
{
GetInputs();
}
private void FixedUpdate()
{
//PlayerJump
if (_jump && _turnedRight == true)
{
transform.rotation = Quaternion.Euler(0, 0, 0);
rb.AddForce(new Vector3(0, jumpForce), ForceMode.Impulse);
_jump = false;
}
else if (_jump && _turnedLeft == true)
{
transform.rotation = Quaternion.Euler(0, -180, 0);
rb.AddForce(new Vector3(0, jumpForce), ForceMode.Impulse);
_jump = false;
}
//RotatePlayerDependingWhereHeMoves
if(_movingLeft == true && isGrounded)
{
gameObject.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
_movingLeft = false;
}
if (_movingRight == true && isGrounded)
{
gameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
_movingRight = false;
}
//MovePlayer
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, _horizontal * -playerSpeed);
//ChecksIfGrounded
GroundCheck();
}
void GetInputs()
{
_horizontal = Input.GetAxisRaw(Tags.HORIZONTAL_AXIS);
if(isGrounded && Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Player pressed Jump");
_jump = true;
}
if(_horizontal == -1)
{
_movingLeft = true;
}
else if(_horizontal == 1)
{
_movingRight = true;
}
if(transform.rotation.y == 0)
{
_turnedRight = true;
}
else if(transform.rotation.y == -180)
{
_turnedLeft = true;
}
}
void GroundCheck()
{
if(Physics.Raycast(transform.position, Vector3.down, distToGround + 0.1f))
{
isGrounded = true;
}
else
{
isGrounded = false;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是Mario PlayControl的脚本代码,希望它会有所帮助,因为您提供了测试代码很难的代码
Here's the script code for Mario PlayControl, hope it helps, as you provide the code to test it's hard
将输入与当前方向进行比较,刚性在空中时正在移动。然后,以所需的速度以速度消失。您可以在另一种方法中执行此操作。
上面,我写了一个示例,该示例应该使您可以降低玩家的速度中间。您可以使用方法
modifymidairsPeed
当玩家不触摸地面以处理中间运动并保持代码分开且整洁时。我还建议将输入存储为vector2。这将使您使用更少的变量(我在谈论
_MovingLeft
和_MovingRight
),并且将来应该证明有用。潜在的问题1
我在您的代码中注意到您修改了刚体速度的Z组件。但是,您将X轴上的精灵翻转,我也将精灵移动到X轴上。这可能是一个潜在的危险疏忽,因为从经典上讲,玩家沿X轴和Z移动用于深度。
解决方案2
我花了一些时间重写代码的部分,以查看是否有其他变量引起怪异行为。我还确保仅修改Z值以避免任何可能的混淆(再次,我建议沿X轴移动...)。尝试一下。
Compare the input to the current direction the rigidbody is moving when mid-air. Then, chip away at the speed at a desired rate. You can do this inside a different method.
Above, I wrote an example which should allow you to reduce the player's velocity mid-air. You can use the method
ModifyMidAirSpeed
when the player is not touching the ground to handle mid-air movement and keep your code separated and tidy.I would also recommend storing input as a Vector2. This will allow you to use fewer variables (I'm talking about
_movingLeft
and_movingRight
) and should prove helpful in the future.Potential problem 1
I noticed in your code you modify the Z component of the rigidbody's velocity. But, you flip the sprite on the X axis and I move the sprite on the X axis too. This could be a potential dangerous oversight, because classically the player moves along the X axis and Z is used for depth.
Solution 2
I spent some time re-writing parts of your code to see if there was some other variable causing the weird behavior. I also made sure to only modify the Z value to avoid any possible confusion (again, I recommend moving along the X axis...). Try this out.