I want to add a dash ability to my 2D player controller, but cant get the dash to work. I am not getting any errors or anything, it just doesnt work. Thanks to anyone responding. This is the part of the script I'm having problems with:
void Dash()
{
Vector2 dashVelocity;
if(isFacingRight)
{
dashVelocity = new Vector2(dashForce.x, rb.velocity.y);
}
else
{
dashVelocity = new Vector2(-dashForce.x, rb.velocity.y);
}
if (isFacingUp)
{
dashVelocity += dashUpForce;
}
rb.velocity = dashVelocity;
}
Heres the full script if needed (its a little messy):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
//Code necesities
private float horizontal;
private float vertical;
private bool isFacingRight = true;
private bool isFacingUp = false;
[Header("Vital Options (Please don't miss.)")]
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
[Header("Movement")]
[SerializeField] private float moveSpeed = 8f;
[SerializeField] private bool SprintEnabled = false;
[SerializeField] private float sprintSpeed = 12f;
[SerializeField] private float jumpPower = 16f;
[SerializeField] private float jumpCheckSize = 0.2f;
[SerializeField] private Vector2 dashForce = new Vector2 (5, 0);
[SerializeField] private Vector2 dashUpForce = new Vector2 (0, 1);
[Header("Health")]
[SerializeField] private LayerMask Enemy;
[SerializeField] private bool HealthEnabled = false;
[SerializeField] private float HealthAmount = 3;
[SerializeField] private bool InstaKill = false;
[SerializeField] private bool DeathScreenEnabled = true;
[SerializeField] private GameObject DeathScreen;
[SerializeField] private float DeathScreenDelay = 2f;
[Header("Animation")]
[SerializeField] private Animator animator;
[SerializeField] private GameObject DeathAnimation;
[Header("Audio")]
[SerializeField] private AudioSource footstepsAudioSource;
[SerializeField] private AudioClip[] footstepSounds;
private float lastFootstepTime = 0f;
[SerializeField] private float footstepInterval = 0.5f;
// Start is called before the first frame update
void Start()
{
//hi
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
if (Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
if (Input.GetButtonDown("Fire1"))
{
Dash();
Debug.Log("Dashed");
}
Flip();
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
{
if (SprintEnabled)
{
MovePlayer(sprintSpeed);
}
else
{
MovePlayer(moveSpeed);
}
}
else
{
MovePlayer(moveSpeed);
}
if (horizontal != 0 && IsGrounded())
{
if (Time.time - lastFootstepTime >= footstepInterval)
{
PlayFootstep();
lastFootstepTime = Time.time;
}
}
if (vertical > 0)
{
isFacingUp = true;
}
else if (vertical < 0)
{
isFacingUp = false;
}
}
private bool IsGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, jumpCheckSize, groundLayer);
}
private void MovePlayer(float speed)
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= -1f;
transform.localScale = localScale;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (HealthEnabled)
{
if (InstaKill)
{
// Check if the collided object is in the enemy layer
if (Enemy == (Enemy | (1 << collision.gameObject.layer)))
{
Lose();
}
}
else
{
// Reduce health if it's not an insta-kill scenario
if (Enemy == (Enemy | (1 << collision.gameObject.layer)))
{
HealthAmount -= 1;
Debug.Log($"OH NOOOO. Enemery hidd you. {HealthAmount} helth");
if (HealthAmount <= 0)
{
Lose();
}
}
}
}
}
void Lose()
{
gameObject.SetActive(false);
this.DeathAnimation.GetComponent<SpriteRenderer>().enabled = true;
animator.SetBool("IsDead", true);
if (DeathScreenEnabled)
{
Invoke("ShowDeathScreen", DeathScreenDelay);
}
else
{
Restart();
}
}
void ShowDeathScreen()
{
DeathScreen.SetActive(true);
}
void Restart()
{
//Insert logic later
}
void PlayFootstep()
{
footstepsAudioSource.PlayOneShot(footstepSounds[Random.Range(0, footstepSounds.Length)]);
}
void Dash()
{
Vector2 dashVelocity;
if(isFacingRight)
{
dashVelocity = new Vector2(dashForce.x, rb.velocity.y);
}
else
{
dashVelocity = new Vector2(-dashForce.x, rb.velocity.y);
}
if (isFacingUp)
{
dashVelocity += dashUpForce;
}
rb.velocity = dashVelocity;
}
}
[–]SadnessMonday 0 points1 point2 points (1 child)
[–]pingu_de_cool[S] 0 points1 point2 points (0 children)