C# 玩家在冲向半空中后几乎无法控制角色
我现在正在开发一个2.5D播放器控制器,我在半空中的冲刺有问题。破折号可以工作,但是在破折号完成后,角色无法在X轴上完全控制,直到它们落地。我希望玩家在冲刺后能够完全控制X轴,这样他们就可以适当地闪避C# 玩家在冲向半空中后几乎无法控制角色,c#,unity3d,game-development,2.5d,C#,Unity3d,Game Development,2.5d,我现在正在开发一个2.5D播放器控制器,我在半空中的冲刺有问题。破折号可以工作,但是在破折号完成后,角色无法在X轴上完全控制,直到它们落地。我希望玩家在冲刺后能够完全控制X轴,这样他们就可以适当地闪避 void Update() { PlayerInput(); } void FixedUpdate() { xMovement(); yMovement(); } void PlayerInput() { //Registers X and
void Update()
{
PlayerInput();
}
void FixedUpdate()
{
xMovement();
yMovement();
}
void PlayerInput()
{
//Registers X and Y movement
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
if (Input.GetButton("Run"))
{
isRunning = true;
}
else if (Input.GetButtonUp("Run"))
{
isRunning = false;
}
//Makes player jump by returning a bool value to "yMovement()" when pressed.
if (Input.GetButtonDown("Jump"))
{
jumpRequest = true;
}
if (Input.GetKeyDown(KeyCode.A))
{
if (doubleTapTime > Time.time && lastKeyCode == KeyCode.A)
{
StartCoroutine(Dash(1f));
Debug.Log("You dashed left");
}
else
{
doubleTapTime = Time.time + 0.5f;
}
lastKeyCode = KeyCode.A;
}
if (Input.GetKeyDown(KeyCode.D))
{
if (doubleTapTime > Time.time && lastKeyCode == KeyCode.D)
{
StartCoroutine(Dash(-1f));
Debug.Log("You dashed right");
}
else
{
doubleTapTime = Time.time + 0.5f;
}
lastKeyCode = KeyCode.D;
}
}
void xMovement()
{
//Makes player walk left and right
if (!isRunning && !isDashing)
{
transform.Translate(Vector3.left * walkSpeed * horizontalInput * Time.deltaTime);
}
//Makes player run left and right
else if (isRunning && !isDashing)
{
transform.Translate(Vector3.left * runSpeed * horizontalInput * Time.deltaTime);
}
}
void yMovement()
{
//Make player jump when Jump is pressed
if (jumpRequest)
{
playerRb.velocity = new Vector3(playerRb.velocity.x, jumpForce);
jumpRequest = false;
//playerRb.velocity = new Vector2(playerRb.velocity.x, playerRb.velocity.y * jumpForce);
}
//Makes player fall faster in general, and when the Jump button is released
if (playerRb.velocity.y < 0)
{
playerRb.velocity += Vector3.up * Physics.gravity.y * (fallMultiplyer - 1) * Time.deltaTime;
}
else if (playerRb.velocity.y > 0 && !Input.GetButton("Jump"))
{
playerRb.velocity += Vector3.up * Physics.gravity.y * (lowJumpMultiplyer - 1) * Time.deltaTime;
}
}
IEnumerator Dash(float direction)
{
isDashing = true;
playerRb.velocity = new Vector3(playerRb.velocity.x, .0f);
playerRb.velocity = new Vector3(dashDistance * direction, 0f, 0f);
playerRb.useGravity = false;
yield return new WaitForSeconds(.2f);
isDashing = false;
playerRb.useGravity = true;
void Update()
{
PlayerInput();
}
void FixedUpdate()
{
xMovement();
运动();
}
void PlayerInput()
{
//寄存器X和Y移动
horizontalInput=Input.GetAxis(“水平”);
verticalInput=Input.GetAxis(“垂直”);
if(Input.GetButton(“Run”))
{
isRunning=true;
}
else if(Input.GetButtonUp(“Run”))
{
isRunning=false;
}
//按下时,通过将布尔值返回到“yMovement()”使玩家跳跃。
if(Input.GetButtonDown(“跳转”))
{
jumpRequest=true;
}
if(Input.GetKeyDown(KeyCode.A))
{
if(doubleTapTime>Time.Time&&lastKeyCode==KeyCode.A)
{
开始例行程序(仪表板(1f));
Log(“您向左虚线”);
}
其他的
{
doubleTapTime=Time.Time+0.5f;
}
lastKeyCode=KeyCode.A;
}
if(Input.GetKeyDown(KeyCode.D))
{
if(doubleTapTime>Time.Time&&lastKeyCode==KeyCode.D)
{
开始例行程序(破折号(-1f));
Log(“您向右虚线”);
}
其他的
{
doubleTapTime=Time.Time+0.5f;
}
lastKeyCode=KeyCode.D;
}
}
无效x移动()
{
//使玩家左右行走
如果(!isRunning&&!isDashing)
{
transform.Translate(Vector3.left*行走速度*水平输入*时间.deltaTime);
}
//让玩家左右跑动
else if(正在运行&!正在灰化)
{
transform.Translate(Vector3.left*运行速度*水平输入*时间.deltaTime);
}
}
无效移动()
{
//按下“跳跃”按钮时使玩家跳跃
如果(跳转请求)
{
playerRb.velocity=新矢量3(playerRb.velocity.x,跳跃力);
jumpRequest=false;
//playerRb.velocity=新矢量2(playerRb.velocity.x,playerRb.velocity.y*跳跃力);
}
//一般来说,当跳转按钮被释放时,使玩家下降得更快
if(播放速度y<0)
{
playerRb.velocity+=Vector3.up*physical.gravity.y*(fallMultiplyer-1)*Time.deltaTime;
}
else if(playerRb.velocity.y>0&&!Input.GetButton(“跳转”))
{
playerRb.velocity+=Vector3.up*物理.gravity.y*(lowJumpMultiplyer-1)*时间.deltaTime;
}
}
IEnumerator仪表板(浮动方向)
{
isDashing=真;
playerRb.velocity=新矢量3(playerRb.velocity.x,.0f);
playerRb.velocity=新矢量3(dashDistance*方向,0f,0f);
playerRb.useGravity=false;
返回新的WaitForSeconds(.2f);
isDashing=false;
playerRb.useGravity=true;
关于代码优化的任何提示都是非常值得赞赏的。我对编码还是相当陌生的,我宁愿先学习适当的编码习惯,然后才能忘记不好的习惯。谢谢!我想你的问题是,你在yAxis上使用的是x轴的变换,而在yAxis上你实际上使用的是速度。Unity在一个“FixedUpdate”调用中可能无法处理这两个问题。或者它可能无法实现您的预期 我建议坚持速度的变化,这样会有
void xMovement()
{
//Makes player walk left and right
if (!isRunning && !isDashing)
{
playerRb.velocity += Vector3.left * walkSpeed * horizontalInput * Time.deltaTime;
}
//Makes player run left and right
else if (isRunning && !isDashing)
{
playerRb.velocity += Vector3.left * runSpeed * horizontalInput * Time.deltaTime;
}
}
您好,您可以尝试添加更多上下文或提供任何类型的演示,其他人可以使用它们来帮助复制和帮助。玩家可以冲刺,只要他们在地面上,冲刺一结束就可以控制。问题是当玩家尝试在半空中冲刺时。玩家可以在空中冲刺,但在完成后没有任何问题在短跑中,运动员在落地之前无法停止冲力或改变方向。这几乎就像短跑在空中“拖拽”他们,直到他们落地。我希望运动员能够保持这种冲力,如果他们愿意的话,但我也不想强迫他们向前移动,直到他们落地。