- Home /
Question by
farhanfaishal · Nov 11, 2019 at 03:01 PM ·
2d2d game2d-platformerrigidbody2d2d-physics
,Hey! I'm trying to make a Dash move like Celeste but my Dash doesn't seem to work Horizontally(X Axis), My Dash is working perfectly fine Upward and Downwards(Y Axis).
Hey! I'm trying to make a Dash move like Celeste but my Dash doesn't seem to work Horizontally(X Axis), My Dash is working perfectly fine Upward and Downwards(Y Axis). Everything else in my code is working perfectly fine. Below is the my code and any help will be appreciated. Thank you!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Move2 : MonoBehaviour {
[SerializeField] public LayerMask whatIsGround;
[Header("Stats")]
float x = 0f;
float y = 0f;
float xRaw = 0f;
float yRaw = 0f;
public float runSpeed = 10f;
Vector2 dir;
public float jumpForce = 10f;
public float dashSpeed = 10f;
public Transform feetPos;
public float checkRadius;
[Space]
[Header("Booleans")]
public bool canjump = false;
private bool isGrounded;
public bool canDash = false;
[HideInInspector]
public Rigidbody2D rb;
void Start ()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
x = Input.GetAxis("Horizontal");
y = Input.GetAxis("Vertical");
xRaw = Input.GetAxisRaw("Horizontal");
yRaw = Input.GetAxisRaw("Vertical");
dir = new Vector2(x, y);
if(Input.GetButtonDown("Jump") && isGrounded == true)
{
canjump = true;
}
if(Input.GetButtonDown("Fire1"))
{
canDash = true;
}
if(dir.x > 0)
{
transform.eulerAngles = new Vector3(0, 0, 0);
}
if(dir.x < 0)
{
transform.eulerAngles = new Vector3(0, 180, 0);
}
}
void FixedUpdate()
{
Walk(dir);
if(canjump == true)
{
Jump(Vector2.up);
}
if(canDash == true)
{
if(xRaw != 0 || yRaw != 0)
{
Dash(xRaw, yRaw);
}
}
}
void Walk(Vector2 dir)
{
rb.velocity = new Vector2(dir.x * runSpeed, rb.velocity.y);
}
private void Jump(Vector2 dir)
{
rb.velocity = new Vector2(rb.velocity.x, 0);
rb.velocity += dir * jumpForce;
canjump = false;
}
void Dash(float x, float y)
{
rb.velocity = Vector2.zero;
Vector2 dir = new Vector2(x, y);
rb.velocity += dir.normalized * dashSpeed;
canDash = false;
}
}
Comment
Answer by Menzle · Apr 04, 2021 at 03:21 PM
Hi, sorry that i can't really help you, I kinda have the same problem, just checking if you found the solution yet.