- Home /
Question by
SuperCrow2 · Jun 27, 2020 at 06:11 AM ·
2d gameunity 2dplatformercodepagedash
How do I get my character to dash up?
I can only get him to dash left and right, idk how to get him to dash up. I dont think you have to sift through the 500+ line code, I tried to make it super quick and easy for everyone by putting the most important lines, the ones that pertains to the dash below. Ignore the commented out stuff, I had to keep that in there when I posted the script here so I wouldnt confuse myself when it comes time to making the up dash work. The commented out dash stuff you may need.
The lines or sections that has the dash: 142 213 224 247 261
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class WizardPlayerController : MonoBehaviour
{
private Renderer rend;
private Color colorToTurnTo = Color.blue;
public float spead;
public float jumpForce = 16.0f; //how high the character jumps
private float movementInputDirection; //the key being pressed down
private float dashTimeLeft; //keep track how much longer the dash should be happening
private float lastImageXpos; //keep track on x coordinate of after image
private float lastDash = -100; //last time we started dash, will be used to check for cool down. -100 so we can dash right when game starts
private float timeLeft; //4 seconds to be off the ground, at the end, drop back down
private float timeLeftSet = 4f; //set our timeLeft at 4 seconds
private float jumpTimer;
private float turnTimer;
private float wallJumpTimer; //this will be part of the code to prevent jumping up a single wall
private int facingDiection = 1; //-1 is left. 1 is right
private int amountOfJumpsLeft;
private int lastWallJumpDirecion; //this will be part of the code to prevent jumping up a single wall
private bool canMove;
private Rigidbody2D rb;
private bool canwallGrab;
private bool isDashing;
private bool dashUp;
private bool facingRight = true;
private bool isGrounded;
private bool canNormalJump;
private bool canWallJump;
private bool isAttemptingToJump;
private bool canFlip;
private bool hasWallJumped; //this will be part of the code to prevent jumping up a single wall
private bool isTouchingWall;
private bool isWallSliding;
private bool checkJumpMultiplier;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private float moveHorizontal;
private float moveVertical;
public float dashTime; //how long the dash should take
public float dashSpeed; //how fast it moves while dashing
public float distanceBetweenImages; //how far the after image game objects should be placed when dashing (the dash effect, it creates images behind him when he dashes)
public float dashCoolDown; //how long till we can dash again
public float movementSpeed = 10.0f;
public int amountOfJumps = 1;
public float wallSlideSpeed;
public float movementForceInAir; //add force to character as its attempting to move in the air
public float airDragMultiplier = 0.95f;
public float variableJumpHeightMultiplier = 0.5f; //tapping space jumps lower than a full press
public Vector2 wallHopDirection;
public Vector2 wallJumpDirection;
public float wallHopForce;
public float wallJumpFoce;
public float jumpTimerSet = 0.15f;
public float turnTimerSet = 0.1f;
public float wallJumpTimerSet = 0.5f; //this will be part of the code to prevent jumping up a single wall
public float groundCheckRadius;
public float wallCheckDistance;
public Transform wallCheck;
private Vector2 speed;
SavePlayerPosition playerPositionData;
//-----------------------------------------------------------------------------
private void Awake()
{
playerPositionData = FindObjectOfType<SavePlayerPosition>(); //finding the script
playerPositionData.PlayerPosLoad();
}
//--------------------------------------------------------------
void Start()
{
rb = GetComponent<Rigidbody2D>();
wallHopDirection.Normalize(); //vector equals 1. when we specify a force, it will always be the force we speified
wallJumpDirection.Normalize();
amountOfJumpsLeft = amountOfJumps;
}
//---------------------------------------------------------
private void Update()
{
CheckInput();
CheckMovementDirection();
CheckIfCanJump();
CheckIfWallSliding();
CheckDash();
CheckJump();
TimeInAir();
WallGrab();
}
//-----------------------------------------------------
private void CheckIfWallSliding()
{
if (isTouchingWall && movementInputDirection == facingDiection && rb.velocity.y < 0) //rb.velocity.y<0 so our character slides down only when going down not when jumping up the wall
{
isWallSliding = true;
}
else
{
isWallSliding = false;
}
}
//---------------------------------------------------------------------------------
private void CheckInput()
{
movementInputDirection = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump"))
{
if (isGrounded || (amountOfJumpsLeft > 0 && isTouchingWall))
{
NormalJump();
}
else
{
jumpTimer = jumpTimerSet;
isAttemptingToJump = true;
}
}
if (Input.GetButtonDown("Horizontal") && isTouchingWall)
{
if (!isGrounded && movementInputDirection != facingDiection)
{
canMove = false;
canFlip = false;
turnTimer = turnTimerSet; //how long to keep it false
}
}
if (!canMove)
{
turnTimer -= Time.deltaTime;
if (turnTimer <= 0)
{
canMove = true;
canFlip = true;
}
}
if (checkJumpMultiplier && !Input.GetButton("Jump")) //space bar. pushing spacebar it will return true. if not pressing it, it will return false
{
checkJumpMultiplier = false;
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * variableJumpHeightMultiplier); //tapping the jump key he jumps lower
}
if (Input.GetButtonDown("Dash")) //z button
{
if (Time.time >= (lastDash + dashCoolDown)) //only dash once cool down is up we can dash again
AttemptToDash();
}
}
//----------------------------------------------------------------------------------
private void AttemptToDash() //start of the dash
{
isDashing = true;
//dashUp = true;
dashTimeLeft = dashTime;
lastDash = Time.time;
// }
// else
// {
// dashUp = false;
// }
PlayerAfterImagePool.Instance.GetFromPool();
lastImageXpos = transform.position.x;
}
//___________________________________________________________________
private void DashUp()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
dashUp = true;
dashTimeLeft = dashTime;
lastDash = Time.time;
PlayerAfterImagePool.Instance.GetFromPool();
lastImageXpos = transform.position.y;
}
}
//---------------------------------------------------------------------------
private void CheckDash()
{
if (isDashing)
{
if (dashTimeLeft > 0)
{
canMove = false;
canFlip = false;
rb.velocity = new Vector2(dashSpeed * facingDiection, 0); //facingDirecion,0 is to get him to dash straight (which is what I wanted to happen). using rb.velocity.y caused him to dash up on a curve like thing
dashTimeLeft -= Time.deltaTime;
}
// if (dashUp == true) //this "if statement" to get him to dash vertically )
// {
// rb.velocity = new Vector2(rb.velocity.x * facingDiection, dashSpeed);
if (Mathf.Abs(transform.position.x - lastImageXpos) > distanceBetweenImages)
{
PlayerAfterImagePool.Instance.GetFromPool();
lastImageXpos = transform.position.x;
}
}
if (dashTimeLeft <= 0 || isTouchingWall)
{
isDashing = false;
canMove = true;
canFlip = true;
}
}
//------------------------------------------------------
private void CheckJump() //I changed jump() to CheckJump()
{
if (jumpTimer > 0)
{
if (!isGrounded && isTouchingWall && movementInputDirection != 0 && movementInputDirection != facingDiection)
{
WallJump(); //wall jum[
}
else if (isGrounded)
{
NormalJump();
}
}
if(isAttemptingToJump)
{
jumpTimer-=Time.deltaTime;
}
if(wallJumpTimer>0)
{
if(hasWallJumped && movementInputDirection== -lastWallJumpDirecion)
{
rb.velocity = new Vector2(rb.velocity.x, 0.0f);
hasWallJumped = false;
} else if(wallJumpTimer<=0)
{
hasWallJumped = false;
}
else
{
wallJumpTimer -= Time.deltaTime;
}
}
}
private void NormalJump() //this and the WllJump() section was combined in the Jump() but now called CheckJump()
{
if (canNormalJump)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce); //jumpforce is our y velocity
amountOfJumpsLeft--;
jumpTimer = 0;
isAttemptingToJump = false;
checkJumpMultiplier = true;
}
}
private void WallJump()
{
if (canWallJump) //wall jumping
{
rb.velocity = new Vector2(rb.velocity.x, 0.0f);
isWallSliding = false;
amountOfJumpsLeft = amountOfJumps;
amountOfJumpsLeft--; //subtract from amount of jumps left (i may have to keep this rmeoved, it was causing him to run out of jumps so he couldnt jump anymore
Vector2 forceToAdd = new Vector2(wallJumpFoce * wallJumpDirection.x * movementInputDirection, wallJumpFoce * wallJumpDirection.y);
rb.AddForce(forceToAdd, ForceMode2D.Impulse);
jumpTimer = 0;
isAttemptingToJump = false;
checkJumpMultiplier = true;
turnTimer = 0;
canMove = true;
canFlip = true;
hasWallJumped = true;
wallJumpTimer = wallJumpTimerSet;
lastWallJumpDirecion = -facingDiection;
}
}
private void FixedUpdate()
{
ApplyMovement();
CheckSurroundings();
}
//------------------------------------------------------------
private void CheckSurroundings()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
isTouchingWall = Physics2D.Raycast(wallCheck.position, transform.right, wallCheckDistance, whatIsGround);
}
//---------------------------------------------------------------------------
private void WallGrab()
{
if(canwallGrab)
{
timeLeft -= Time.deltaTime;
}
}
private void TimeInAir() //the time he's not on the ground
{
}
private void CheckIfCanJump() //check to see if he can jump
{
if (isGrounded && rb.velocity.y <= 0.01f)
{
amountOfJumpsLeft = amountOfJumps;
}
if (isTouchingWall)
{
canWallJump = true;
}
if (amountOfJumpsLeft <= 0)
{
canNormalJump = false;
}
else
{
canNormalJump = true;
}
}
//--------------------------------------------------------------------------
private void ApplyMovement()
{
if (!isGrounded && !isWallSliding && movementInputDirection == 0) //when we jump straight up, we can't move until it touches ground. //I asusme when movementInoutDirecion==0 that means hes not moving in any direction
{
rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
}
else if (canMove)
{ //apply the force
rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
}
//Vector2 forceToAdd = new Vector2(movementForceInAir * movementInputDirection, 0);
//rb.AddForce(forceToAdd);
if (isWallSliding)
{
if (rb.velocity.y < -wallSlideSpeed)
{
rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
}
}
}
//if (Mathf.Abs(rb.velocity.x) > movementSpeed)
//{
// rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);
// }
//}
//I assume when movementInoutDirecion==0 that means hes not moving in any direction
// {
// rb.velocity = new Vector2(rb.velocity.x * airDragMultiplier, rb.velocity.y);//keep y velocicty the same. //slows the guy down
// }
//if (isWallSliding)
// {
//if (rb.velocity.y < -wallSlideSpeed)
//{
// rb.velocity = new Vector2(rb.velocity.x, -wallSlideSpeed);
//// }
// }
// }
//(!isGrounded && !isWallSliding && movementInputDirection != 0) //not is grounded and not is wall sliding
//----------------------------------------------------------------------------------
private void CheckMovementDirection() //moves him right or left. flips him so he faces the direction he is going in
{
if (facingRight && movementInputDirection < 0)
{
Flip();
}
else if (!facingRight && movementInputDirection > 0) //because he is moving left while facing right
{
Flip();
}
}
void ChangeColor()
{
if (dashCoolDown<=2.0f)
{
rend.material.color = colorToTurnTo;
}
}
private void Flip()
{
if (!isWallSliding && canFlip)
{
facingDiection *= -1; //facing left. every time we flip the character it will go back and forth between -1 and 1
facingRight = !facingRight;
transform.Rotate(0.0f, 180.0f, 0.0f); //x, y, z
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
Gizmos.DrawLine(wallCheck.position, new Vector3(wallCheck.position.x + wallCheckDistance, wallCheck.position.y, wallCheck.position.z));
}
}
Comment