Unity3d 单位2D不一致的附加力
我最近刚问了另一个问题,我很快就得到了回答,所以我希望这一个问题也一样。不管怎样,我正在制作这个2D平板电脑(超级肉食男孩一样),我添加了这个很酷的“冲刺”功能,当你按下Shift键时就会触发。它会增加一个x轴的力和一点y轴的力,给它一种跳跃的感觉。然而,AddForce函数在游戏中是如此的不一致。有时,y轴力大于x轴力,有时x轴力增加一半(?)。我能想到为什么会发生这种情况的唯一原因是,我增加了适当的不现实但好看的跳跃的重力,当运动员冲刺时,重力会稍微增加一些。或者,当我在跳跃的顶峰冲刺时,会有比冲刺开始时更强的力量吗?我很困惑 下面是update()函数中的所有内容Unity3d 单位2D不一致的附加力,unity3d,Unity3d,我最近刚问了另一个问题,我很快就得到了回答,所以我希望这一个问题也一样。不管怎样,我正在制作这个2D平板电脑(超级肉食男孩一样),我添加了这个很酷的“冲刺”功能,当你按下Shift键时就会触发。它会增加一个x轴的力和一点y轴的力,给它一种跳跃的感觉。然而,AddForce函数在游戏中是如此的不一致。有时,y轴力大于x轴力,有时x轴力增加一半(?)。我能想到为什么会发生这种情况的唯一原因是,我增加了适当的不现实但好看的跳跃的重力,当运动员冲刺时,重力会稍微增加一些。或者,当我在跳跃的顶峰冲刺时,会
void Update()
{
//Flipping and moving lightfeet
if (Input.GetKey(KeyCode.A))
{
anim.SetFloat("Horizontal", 1);
x = -1;
scale.x = -4;
rb.velocity = new Vector2(-speed, rb.velocity.y);
}
else if (Input.GetKey(KeyCode.D))
{
anim.SetFloat("Horizontal", 1);
x = 1;
scale.x = 4;
rb.velocity = new Vector2(speed, rb.velocity.y);
}
else //no keys pressed
{
anim.SetFloat("Horizontal", 0);
rb.velocity = new Vector2(0f, rb.velocity.y);
anim.SetBool("Sprint", false); //even after sprinting and then stopping before to jump, sprint anim wont play
}
transform.localScale = scale;
//Accumulating jump charge and increasing speed!
if (Input.GetKey(KeyCode.Space) && rb.velocity.y == 0f) //only if space is held down and there is no vertical movement
{
jumpAccumulator += Time.deltaTime * 25f; //accumulates jump power over time
if (jumpAccumulator >= maxJumpAccumulator) //maximum jump power
{
jumpAccumulator = maxJumpAccumulator;
}
speed *= sprintSpeed; //increase speed when charging up
if(speed >= maxSpeed) //maximum speed
{
speed = maxSpeed;
}
}
//Ground detection
groundHit = Physics2D.BoxCast(bcollider.bounds.center, bcollider.bounds.size, 0f, Vector2.down, 0.1f ,groundmask);
mapHit = Physics2D.BoxCast(bcollider.bounds.center, bcollider.bounds.size, 0f, Vector2.down, 0.1f, mapMask);
if (groundHit.collider != null || mapHit.collider != null) //When you on the ground!
{
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", false);
anim.SetBool("Dashed", false);
anim.SetBool("Fall", false);
canJump = true;
canDoubleJump = true;
canDash = false;
rb.gravityScale = 15; //nomral gravity scale
//animations for running or sprinting
if (speed <= initialspeed)
{
anim.SetBool("Sprint", false);
}
else if (speed > initialspeed && rb.velocity.x > initialspeed && rb.velocity.y == 0 || speed > initialspeed && rb.velocity.x < -initialspeed)
{
anim.SetBool("Sprint", true);
}
}
else if (groundHit.collider == null || mapHit.collider == null) //if LF is not on ground!
{
canJump = false;
anim.SetFloat("Horizontal", 0f);
}
//falling without jumping
if (rb.velocity.y < 0 && anim.GetBool("Jumped") == false && anim.GetBool("DoubleJumped") == false && anim.GetBool("Dashed") == false)
{
anim.SetBool("Fall", true);
anim.SetBool("Sprint", false);
canDash = true;
}
JumpDash();
}
void JumpDash()
{
//Jump
if (Input.GetKeyUp(KeyCode.Space) && canJump == true)
{
rb.velocity = Vector2.up * (jumpForce + jumpAccumulator);
anim.SetBool("Jumped", true);
anim.SetBool("DoubleJumped", false); //setting false to prevent bugs
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
canDash = true;
jumpAccumulator = 1;
}
//Double Jump
if(Input.GetKeyDown(KeyCode.W) && canJump == false && canDoubleJump == true)
{
rb.velocity = Vector2.up * jumpForce;
canDoubleJump = false;
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", true);
anim.SetBool("Dashed", false); //disabling dash animation so it doesn't play when dashing then double jumping (because conditions for both can be true at the same time)
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
anim.SetBool("Fall", false);
doubleJumpFX.Play();
DashFX.Stop();
}
//Dash
if(Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
{
rb.AddForce(new Vector2(dashForce * x, dashUpForce), ForceMode2D.Impulse);
rb.gravityScale += dashGravity; //increasing gravity for dashing
canDash = false;
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", false);
anim.SetBool("Dashed", true);
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
doubleJumpFX.Stop();
DashFX.Play();
}
}
void OnCollisionEnter2D(Collision2D collision)
{
Debug.Log("Collider: " + collision.collider.name);
if (collision.collider.name == "platform" || collision.collider.name == "mapColliders")
{
speed = initialspeed; //set speed to normal on hitting the ground (difference between this and raycast is this returns true for only one frame)
anim.SetBool("Dashed", false); //setting false to this anim here again to prevent buggy animations
anim.SetBool("Fall", false);
doubleJumpFX.Stop();
DashFX.Stop();
}
if (collision.collider.name == "Respawn area")
{
transform.position = new Vector2(-13f, 4f);
}
}
void Update()
{
//翻转和移动光脚
if(Input.GetKey(KeyCode.A))
{
动画设置浮动(“水平”,1);
x=-1;
标度x=-4;
rb.velocity=新矢量2(-speed,rb.velocity.y);
}
else if(Input.GetKey(KeyCode.D))
{
动画设置浮动(“水平”,1);
x=1;
比例x=4;
rb.velocity=新矢量2(速度,rb.velocity.y);
}
else//没有按键
{
动画设置浮动(“水平”,0);
rb.velocity=新矢量2(0f,rb.velocity.y);
SetBool(“Sprint”,false);//即使在冲刺之后,然后在跳跃之前停下来,Sprint动画也不会玩
}
transform.localScale=scale;
//积累跳跃电荷,提高速度!
if(Input.GetKey(KeyCode.Space)&&rb.velocity.y==0f)//仅当按住空格且没有垂直移动时
{
jumpAccumulator+=Time.deltaTime*25f;//随时间累积跳跃功率
if(jumpAccumulator>=maxJumpAccumulator)//最大跳跃功率
{
跳线累加器=最大跳线累加器;
}
速度*=冲刺速度;//充电时提高速度
如果(速度>=最大速度)//最大速度
{
速度=最大速度;
}
}
//地面探测
groundHit=Physics2D.BoxCast(bcollider.bounds.center,bcollider.bounds.size,0f,矢量2.down,0.1f,地面掩码);
mapHit=Physics2D.BoxCast(bcollider.bounds.center、bcollider.bounds.size、0f、Vector2.down、0.1f、mapMask);
如果(groundHit.collider!=null | | mapHit.collider!=null)//当你在地面上时!
{
动画挫折(“跳跃”,假);
动画挫折(“双重跳跃”,假);
动画设置工具(“虚线”,假);
动画挫折(“跌倒”,错误);
canJump=true;
canDoubleJump=真;
坎达斯=假;
rb.gravityScale=15;//标称重力刻度
//用于跑步或短跑的动画
如果(速度初始速度和rb.velocity.x>初始速度和rb.velocity.y==0 | |速度>初始速度和rb.velocity.x<-初始速度)
{
动画挫折(“冲刺”,真实);
}
}
else if(groundHit.collider==null | | mapHit.collider==null)//如果LF不在地面上!
{
canJump=false;
动画设置浮动(“水平”,0f);
}
//不跳不落
if(rb.velocity.y<0&&anim.GetBool(“跳跃”)==false&&anim.GetBool(“双跳跃”)==false&&anim.GetBool(“虚线”)==false)
{
动画挫折(“跌倒”,真实);
动画SetBool(“Sprint”,false);
坎达斯=真;
}
JumpDash();
}
void JumpDash()
{
//跳跃
if(Input.GetKeyUp(KeyCode.Space)和&canJump==true)
{
rb.velocity=Vector2.up*(跳跃力+跳跃累加器);
动画挫折(“跳跃”,真实);
SetBool(“DoubleJumped”,false);//设置false以防止bug
SetBool(“Sprint”,false);//此处也设置false以进行Sprint,以防止出现bug
坎达斯=真;
跳跃式累加器=1;
}
//双跳
if(Input.GetKeyDown(KeyCode.W)和&canJump==false和&canDoubleJump==true)
{
rb.速度=矢量2.向上*跳跃力;
canDoubleJump=假;
动画挫折(“跳跃”,假);
动画挫折(“双重跳跃”,真实);
anim.SetBool(“虚线”,false);//禁用虚线动画,使其在冲刺时不播放,然后双击(因为两者的条件可以同时为真)
SetBool(“Sprint”,false);//此处也设置false以进行Sprint,以防止出现bug
动画挫折(“跌倒”,错误);
doubleJumpFX.Play();
DashFX.Stop();
}
//冲刺
if(Input.GetKeyDown(KeyCode.LeftShift)和&canDash==true)
{
rb.AddForce(新矢量2(dashForce*x,dashUpForce),ForceMode2D.pulse);
rb.gravityScale+=dashGravity;//增加冲刺的重力
坎达斯=假;
动画挫折(“跳跃”,假);
动画挫折(“双重跳跃”,假);
动画设置工具(“虚线”,真);
SetBool(“Sprint”,false);//此处也设置false以进行Sprint,以防止出现bug
doubleJumpFX.Stop();
DashFX.Play();
}
}
空心OnCollisionInter2D(碰撞2D碰撞)
{
Log(“Collider:+collide.Collider.name”);
if(collision.collider.name==“platform”| | collision.collider.name==“mapColliders”)
{
速度=初始速度;//在撞击地面时将速度设置为正常(此与光线投射的区别在于此ret
rb.velocity = new Vector2 (0, 0);
//Dash
if(Input.GetKeyDown(KeyCode.LeftShift) && canDash == true)
{
rb.velocity = new Vector2 (0, 0);
rb.AddForce(new Vector2(dashForce * x, dashUpForce), ForceMode2D.Impulse);
rb.gravityScale += dashGravity; //increasing gravity for dashing
canDash = false;
anim.SetBool("Jumped", false);
anim.SetBool("DoubleJumped", false);
anim.SetBool("Dashed", true);
anim.SetBool("Sprint", false); //setting false to sprint here as well just to prevent bugs
doubleJumpFX.Stop();
DashFX.Play();
}