여기에 완전한 플레이어 컨트롤러 파일이 있습니다.
using UnityEngine;
public class PlayerCont : MonoBehaviour
{
[Header("Movement Variables")]
public float moveSpeed;
private float speed;
public float maxSpeed;
public float acceleration;
public float dropSpeed;
public float slideSpeed;
public int direction= 1;
[Header("Jump Variables")]
public float jumpForce;
public int extraJumps;
private float jumpTimeCounter;
public float jumpTime;
private bool isJumping;
private int jumpNum;
[Header("Dash Variables")]
public float dashSpeed;
public float dashLength= .5f;
public float dashCooldown= 1f;
private float dashCounter;
public float dashCoolCounter;
private KeyCode _lastKeyCode;
private float doubleTapTime= 0.05f;
private bool doubleTap;
public bool isDashing;
public float dashTime;
public float maxDashTime;
[Header("GroundCheck Variables")]
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
[Header("Debug Variables")]
public Rigidbody2D rb;
public float moveInputX;
public float moveInputY;
public bool facingRight= true;
public bool isGrounded;
public bool downKey;
public float rbxVel;
//Start is called before the first frame update
void Start()
{
rb= GetComponent<Rigidbody2D>();
speed= moveSpeed;
jumpNum= extraJumps;
}
void FixedUpdate()
{
}
//Update is called once per frame
void Update()
{
moveInputX= Input.GetAxisRaw("Horizontal");
moveInputY= Input.GetAxisRaw("Vertical");
isGrounded= Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
KeyChecks();
Jump();
GroundPound();
Move();
Dash();
}
void KeyChecks()
{
//restricts movement in Y to only be downward
if (moveInputY > 0)
{
moveInputY= 0;
}
//checks for player drop
if (!isGrounded &
&
moveInputY < 0)
{
downKey= true;
}
else
{
downKey= false;
}
//changes direction relative to keypress
if (Input.GetKeyDown(KeyCode.A))
{
direction= -1;
}
if (Input.GetKeyDown(KeyCode.D))
{
direction= 1;
}
}
void Jump()
{
//sets jump number to user specified amount after touching ground
if (isGrounded)
{
extraJumps= jumpNum;
}
//jumps and decrements extrajumps by 1
if (Input.GetKeyDown(KeyCode.Space) &
&
!downKey &
&
extraJumps > 0)
{
isJumping= true;
rb.velocity= new Vector2(rb.velocity.x, jumpForce);
extraJumps--;
}
//basic jump off of ground
else if (Input.GetKeyDown(KeyCode.Space) &
&
extraJumps== 0 &
&
isGrounded &
&
!downKey)
{
isJumping= true;
rb.velocity= new Vector2(rb.velocity.x, jumpForce);
}
//checks for longkeypress of spacebar and increases length of jump
if (Input.GetKey(KeyCode.Space) &
&
extraJumps > 1 &
&
isJumping) //allows a hold down increase to jump while grounded
{
if (jumpTimeCounter > 0)
{
rb.velocity= new Vector2(rb.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
else
{
jumpTimeCounter= jumpTime;
isJumping= false;
}
}
}
void GroundPound()
{
//ground pound keypress check
if (!isGrounded &
&
Input.GetKeyDown(KeyCode.S))
{
if (!doubleTap &
&
doubleTapTime > Time.time &
&
_lastKeyCode== KeyCode.S)
{
rb.velocity= new Vector2(rb.velocity.x, -1 * dropSpeed);
Debug.Log("dashing right");
}
else
{
doubleTapTime= Time.time + 0.5f;
}
_lastKeyCode= KeyCode.S;
}
}
void Move()
{
//changes movement relative to input
if (!isDashing)
{
if (isGrounded &
&
Input.GetKey(KeyCode.S))
{
rb.velocity= new Vector2(rb.velocity.x * .999f, rb.velocity.y);
}
else if (moveInputX== 0 &
&
isGrounded)
{
speed= moveSpeed;
rb.velocity= new Vector2(rb.velocity.x * .99f, rb.velocity.y);
}
else if (moveInputX== 0 &
&
!isGrounded)
{
speed= moveSpeed;
}
else
{
rb.velocity= new Vector2(moveInputX * speed, moveInputY /10 + rb.velocity.y);
}
}
//checks player velocity
rbxVel= rb.velocity.x;
//increases speed relative to input direction
if (moveInputX > 0 || moveInputX < 0)
{
speed += acceleration;
}
//caps speed
if (speed > maxSpeed)
{
speed= maxSpeed;
}
//caps min speed
if (speed < moveSpeed)
{
speed= moveSpeed;
}
}
void Dash()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
isDashing= true;
if (isDashing &
&
dashCoolCounter <= 0)
{
dashTime -= Time.deltaTime;
dashCoolCounter= dashCooldown;
rb.velocity= new Vector2(dashSpeed * direction, rb.velocity.y);
Debug.Log("Dash Started");
}
}
else
{
dashTime= maxDashTime;
isDashing= false;
Debug.Log("No Dash");
}
//resets dash cooldown
if (dashCoolCounter > 0)
{
dashCoolCounter -= Time.deltaTime;
}
}
void Flip()
{
facingRight= !facingRight;
Vector3 Scaler= transform.localScale;
Scaler.x *= -1;
transform.localScale= Scaler;
}
}
이동용 키(A,S) 사용 시 대시가 작동하지 않습니다. 이상하게도 code가 실행 중이지만(콘솔에서 디버그 문이 표시됨) 물리적으로 아무것도 변경되지 않습니다.
대시 code를 rb.velocity= new Vector2(dashSpeed * direction, rb.velocity.y)에서 rb.velocity = dashspeed<로 다시 작성할 때 /em>대시는 문제 없이 작동합니다. 그것의 유일한 단점은 RigidBody의 Y 구성 요소도 조작된다는 것입니다.
이것은 며칠 동안 문제가 되었고, 이제 막 게시물을 다시 작성하고 있습니다.
어떤 도움이든 환영합니다.
- 답변 # 1
isDashing= false를 너무 빨리 설정하고 있습니다. Move가 code를 실행하고 리지드바디의 속도를 정상 이동으로 다시 변경할 수 있도록 하는 Shift를 놓는 즉시 false로 설정됩니다.
테스트는 하지 않았지만 한 번 시도해 보고 원하는 대로 작동하는지 확인하십시오.
void Dash() { if (Input.GetKeyDown(KeyCode.LeftShift)) { if (!isDashing & & dashCoolCounter <= 0) { isDashing= true; dashTime= 0; dashCoolCounter= dashCooldown; rb.velocity= new Vector2(dashSpeed * direction, rb.velocity.y); Debug.Log("Dash Started"); } else if(isDashing) { rb.velocity= new Vector2(dashSpeed * direction, rb.velocity.y); dashTime += Time.deltaTime; if(dashTime >= maxDashTime) { isDashing= false; Debug.Log("No Dash"); } } } //resets dash cooldown if (dashCoolCounter > 0) { dashCoolCounter -= Time.deltaTime; } }
그리고 나는 단지 포스트를 다시 쓰고 있다. .. 나는 그것을 보았고 그것이 내 downvote의 이유입니다. 같은 질문을 삭제하고 다시 게시하지 마세요! 오히려 콘텐츠 품질에 노력하십시오. 당신은 거대한 code를 제공하고 그것에 문제가 있다고 알려주십시오 .. 문제를 재현하는 데 필요한 최소 code로 줄일 수 있습니까? 나는 예를 들어 의심한다. Flip, Jump, GroundPound는 모두 귀하의 문제와 관련이 있습니다 ...
derHugo2022-01-25 08:54:13