问题描述
在 Unity 中为我的游戏编写 2D 运动时,我遇到了一个问题。以前我决定让角色不能在空中移动,但是,我想改变这一点。我该怎么做?我希望角色能够在空中转动方向,但速度与“moveSpeed”不同。这是我第一次在这个网站上,所以如果我泄露了太多细节,我深表歉意。以下是移动和跳跃脚本:
行走脚本
public bool canMove = true;
[Serializefield] public Vector2 newVeLocity;
[Serializefield] public Rigidbody2D rb;
[Serializefield] public float moveSpeed = 10f;
[Serializefield] public Vector2 direction;
[Serializefield] public float wallSlideSpeed;
public float movementForceAir;
public float airDragMultiplier;
FlipChar flipc;
GroundCheck gc;
SlopeWalk sw;
PlayerJump pj;
WallChecker wc;
DashAfterimage da;
void Awake()
{
gc = GetComponent<GroundCheck>();
sw = GetComponent<SlopeWalk>();
rb = GetComponent<Rigidbody2D>();
pj = GetComponent<PlayerJump>();
wc = GetComponent<WallChecker>();
da = GetComponent<DashAfterimage>();
}
void Start()
{
flipc = GetComponent<FlipChar>();
}
void Update()
{
}
void FixedUpdate()
{
if (gc.onGround && canMove && !da.dashing)
{
rb.veLocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed,rb.veLocity.y);
}
else if (!gc.onGround && !wc.isWallSliding && Input.GetAxis("Horizontal") != 0)
{
Vector2 forcetoAdd = new Vector2(movementForceAir * Input.GetAxis("Horizontal"),0);
rb.AddForce(forcetoAdd);
if (Mathf.Abs(rb.veLocity.x) > moveSpeed)
{
rb.veLocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"),rb.veLocity.y);
// }else if(!gc.onGround && !wc.isWallSliding && Input.GetAxis("Horizontal") == 0){
// rb.veLocity = new Vector2(rb.veLocity.x,rb.veLocity.y);
}
}
if (wc.isWallSliding)
{
if (rb.veLocity.y < wallSlideSpeed)
{
rb.veLocity = new Vector2(rb.veLocity.x,-wallSlideSpeed);
}
}
}
}
}
跳转脚本
[HideInInspector] public GroundCheck gc;
[HideInInspector] public PlayerWalk pw;
[Serializefield] private float jumpForce;
[Serializefield] private float jumpTimeCounter;
[Serializefield] private float jumpTime;
[Serializefield] private float extraJumps;
[Serializefield] private float extraJumpsValue;
[Serializefield] private float variableJumpHeight = 0.5f;
public bool isJumping;
public bool canJump;
WallJumpCode wjc;
WallChecker wc;
FlipChar fc;
DashAfterimage da;
void Start()
{
wjc = GetComponent<WallJumpCode>();
pw = GetComponent<PlayerWalk>();
gc = GetComponent<GroundCheck>();
fc = GetComponent<FlipChar>();
extraJumps = extraJumpsValue;
da = GetComponent<DashAfterimage>();
wc = GetComponent<WallChecker>();
}
void Update()
{
isJumping = true;
if (pw.rb.veLocity.y <= 0.0f)
{
isJumping = false;
}
if ((gc.onGround && pw.rb.veLocity.y <= 0.0f) || wc.isWallSliding)
{
extraJumps = extraJumpsValue;
}
if ((Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && !wc.isWallSliding))
{
pw.rb.veLocity = new Vector2(pw.rb.veLocity.x,jumpForce);
extraJumps--;
}
else if (wc.isWallSliding && Input.GetKeyDown(KeyCode.Space) && !isJumping)
{
wc.isWallSliding = false;
extraJumps--;
Vector2 forcetoAdd = new Vector2(wjc.wallHopForce * wjc.wallHopDirection.x * -wjc.facingDirection,wjc.wallHopForce * wjc.wallHopDirection.y);
pw.rb.AddForce(forcetoAdd,ForceMode2D.Impulse);
}
// else if ((wc.isWallSliding ) && Input.GetAxis("Horizontal") > 0 && !isJumping)
// {
// wc.isWallSliding = false;
// extraJumps--;
// Vector2 forcetoAdd = new Vector2(wjc.wallJumpForce * wjc.wallJumpDirection.x * Input.GetAxis("Horizontal"),wjc.wallJumpForce * wjc.wallJumpDirection.y);
//pw.rb.AddForce(forcetoAdd,ForceMode2D.Impulse);
// }
if (Input.GetButtonUp("Jump"))
{
pw.rb.veLocity = new Vector2(pw.rb.veLocity.x,pw.rb.veLocity.y * variableJumpHeight);
}
}
private void Jump()
{
if (!wc.isWallSliding && canJump)
{
pw.rb.veLocity = new Vector2(pw.rb.veLocity.x,jumpForce); ;
extraJumps--;
}
else if (wc.isWallSliding && Input.GetAxis("Horizontal") == 0 && canJump) //Wall hop
{
wc.isWallSliding = false;
extraJumps--;
Vector2 forcetoAdd = new Vector2(wjc.wallHopForce * wjc.wallHopDirection.x * -wjc.facingDirection,ForceMode2D.Impulse);
}
else if ((wc.isWallSliding || wc.isTouchingWall) && Input.GetAxis("Horizontal") != 0 && canJump)
{
wc.isWallSliding = false;
extraJumps--;
Vector2 forcetoAdd = new Vector2(wjc.wallJumpForce * wjc.wallJumpDirection.x * Input.GetAxis("Horizontal"),wjc.wallJumpForce * wjc.wallJumpDirection.y);
pw.rb.AddForce(forcetoAdd,ForceMode2D.Impulse);
}
}
}
}
解决方法
我会使用 2d colliders 来检查您是否接触到了地面物体。然后在你的移动方法上,我会检查你是否在触摸它。如果不是,请更改移动速度。