- Home /
Dash towards player direction
Hi, im making a "Celeste" type game, i want to implement a dash mechanic that makes the player move towards the direction he is looking at (8 directions, also diagonals) for gamepad (controller) all with the same button, depending on the keys that you pressed (a + w + dash key = ⬉ ). Can someone help me?, this is my current code (dash not implemented yet)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
[Header("Components")]
private Rigidbody2D rb;
[Header("Layer Masks")]
[SerializeField] private LayerMask groundLayer;
[Header("Movement")]
[SerializeField] private float movementAcceleration;
[SerializeField] private float maxMoveSpeed;
[SerializeField] private float groundLinearDrag;
private float horizontalDirection;
private bool changingDirection => (rb.velocity.x > 0f && horizontalDirection < 0f) || (rb.velocity.x < 0f && horizontalDirection > 0f);
[Header("Jump")]
[SerializeField] private float jumpForce = 12f;
[SerializeField] private float airLinearDrag = 2.5f;
[SerializeField] private float fallMultiplier = 8f;
[SerializeField] private float lowJumpMultiplier = 5f;
[SerializeField] private float hangTime = 0.1f;
[SerializeField] private float jumpBufferLength = 0.1f;
private float hangTimeCounter;
public float jumpBufferCounter;
private bool canJump => jumpBufferCounter > 0 && hangTimeCounter > 0f;
[Header("Ground Collision")]
[SerializeField] private float groundRaycastLenght;
[SerializeField] private Vector3 groundRaycastOffset;
private bool onGround;
[Header("Corner Correction")]
[SerializeField] private float topRaycastLength;
[SerializeField] private Vector3 edgeRaycastOffset;
[SerializeField] private Vector3 innerRaycastOffset;
private bool canCornerCorrect;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
private void Update()
{
horizontalDirection = GetInput().x;
//Input.GetButtonDown("Jump")
if (Input.GetButtonDown("Jump"))
{
jumpBufferCounter = jumpBufferLength;
}
else
{
jumpBufferCounter -= Time.deltaTime;
}
}
private void FixedUpdate()
{
CheckCollisions();
MoveCharacter();
if (onGround)
{
ApplyGroundLinearDrag();
hangTimeCounter = hangTime;
hangTime = 0.15f;
maxMoveSpeed = 8;
}
else
{
ApplyAirLinearDrag();
FallMultiplier();
hangTimeCounter -= Time.fixedDeltaTime;
}
if (canJump) Jump();
if (canCornerCorrect) CornerCorrect(rb.velocity.y);
}
private Vector2 GetInput()
{
return new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
}
private void MoveCharacter()
{
rb.AddForce(new Vector2(horizontalDirection, 0f) * movementAcceleration);
if (Mathf.Abs(rb.velocity.x) > maxMoveSpeed)
{
rb.velocity = new Vector2(Mathf.Sign(rb.velocity.x) * maxMoveSpeed, rb.velocity.y);
}
}
private void ApplyGroundLinearDrag()
{
if (Mathf.Abs(horizontalDirection) < 0.4f || changingDirection)
{
rb.drag = groundLinearDrag;
}
else
{
rb.drag = 10f;
}
}
private void ApplyAirLinearDrag()
{
rb.drag = airLinearDrag;
}
private void Jump()
{
rb.velocity = new Vector2(rb.velocity.x, 0f);
ApplyAirLinearDrag();
rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
hangTime = -1f;
jumpBufferCounter = .1f;
}
private void Dash()
{
}
private void CheckCollisions()
{
//GROUND COLLISIONS
onGround = Physics2D.Raycast(transform.position + groundRaycastOffset, Vector2.down, groundRaycastLenght, groundLayer) ||
Physics2D.Raycast(transform.position - groundRaycastOffset, Vector2.down, groundRaycastLenght, groundLayer);
//CORNER COLLISIONS
canCornerCorrect = Physics2D.Raycast(transform.position + edgeRaycastOffset, Vector2.up, topRaycastLength, groundLayer) &&
!Physics2D.Raycast(transform.position + innerRaycastOffset, Vector2.up, topRaycastLength, groundLayer) ||
Physics2D.Raycast(transform.position - edgeRaycastOffset, Vector2.up, topRaycastLength, groundLayer) &&
!Physics2D.Raycast(transform.position - innerRaycastOffset, Vector2.up, topRaycastLength, groundLayer);
}
private void FallMultiplier()
{
if (rb.velocity.y < 0)
{
rb.gravityScale = fallMultiplier;
}
else if (rb.velocity.y > 0 && !Input.GetButtonDown("Jump"))
{
rb.gravityScale = lowJumpMultiplier;
}
else
{
rb.gravityScale = 1f;
}
}
private void CornerCorrect(float Yvelocity)
{
//PUSH PLAYER TO THE RIGHT
RaycastHit2D hit = Physics2D.Raycast(transform.position - innerRaycastOffset + Vector3.up * topRaycastLength, Vector3.left, topRaycastLength, groundLayer);
if (hit.collider != null)
{
float newPos = Vector3.Distance(new Vector3(hit.point.x, transform.position.y, 0f) + Vector3.up * topRaycastLength,
transform.position - edgeRaycastOffset + Vector3.up * topRaycastLength);
transform.position = new Vector3(transform.position.x + newPos, transform.position.y, transform.position.z);
rb.velocity = new Vector2(rb.velocity.x, Yvelocity);
return;
}
//PUSH PLAYER TO THE LEFT
hit = Physics2D.Raycast(transform.position + innerRaycastOffset + Vector3.up * topRaycastLength, Vector3.right, topRaycastLength, groundLayer);
if (hit.collider != null)
{
float newPos = Vector3.Distance(new Vector3(hit.point.x, transform.position.y, 0f) + Vector3.up * topRaycastLength,
transform.position + edgeRaycastOffset + Vector3.up * topRaycastLength);
transform.position = new Vector3(transform.position.x - newPos, transform.position.y, transform.position.z);
rb.velocity = new Vector2(rb.velocity.x, Yvelocity);
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
//GROUND CHECK
Gizmos.DrawLine(transform.position + groundRaycastOffset, transform.position + groundRaycastOffset + Vector3.down * groundRaycastLenght);
Gizmos.DrawLine(transform.position - groundRaycastOffset, transform.position - groundRaycastOffset + Vector3.down * groundRaycastLenght);
//CORNER CHECK
Gizmos.DrawLine(transform.position + edgeRaycastOffset, transform.position + edgeRaycastOffset + Vector3.up * topRaycastLength);
Gizmos.DrawLine(transform.position - edgeRaycastOffset, transform.position - edgeRaycastOffset + Vector3.up * topRaycastLength);
Gizmos.DrawLine(transform.position + innerRaycastOffset, transform.position + innerRaycastOffset + Vector3.up * topRaycastLength);
Gizmos.DrawLine(transform.position - innerRaycastOffset, transform.position - innerRaycastOffset + Vector3.up * topRaycastLength);
//CORNER DISTANCE CHECK
Gizmos.DrawLine(transform.position - innerRaycastOffset + Vector3.up * topRaycastLength,
(transform.position - innerRaycastOffset + Vector3.up * topRaycastLength + Vector3.left * topRaycastLength));
Gizmos.DrawLine(transform.position + innerRaycastOffset + Vector3.up * topRaycastLength,
(transform.position + innerRaycastOffset + Vector3.up * topRaycastLength + Vector3.right * topRaycastLength));
}
}
Answer by Feldspatt · May 15, 2021 at 01:45 PM
You coud look into the new Input System, new unity API for inputs. It tights input (one or multiple keys) to actions.
It would externalize the dash input from the class, and in the class you just have the
OnDash(Vector2 direction){
//dashlogic
}.
Plus if you want multi-platform it makes it easier.
Your answer

Follow this Question
Related Questions
How do I make a dash attack while doing a top down 2d game 1 Answer
Adding 1 frame of force 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Movement Problems PS4 Controller 0 Answers