2.5D Platformer - Dashing towards the character is moving.,2.5D platformer - Dashing left and right while moving
Heya, Im pretty new to coding with C# (done the learning modules on Unitys website) and now trying to make my own projects. Got this 2.5D platformer Im trying to make and Im a little stuck with the Dash ability.
This is probably me being stupid and a really easy fix but I would like to Dash by pressing left shift and dash towards the direction the character is going (i.e going left when pressing down A and going right when pressing down D). I have managed to get the actual Dash to work but its only moving to the right. I know it is because I have written "playerRb.velocity = transform.right * dashSpeed;" but how do I write so it goes to the direction of where my character is currently going?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
private Vector3 movement;
private Rigidbody playerRb;
public float jumpForce;
private const int maxJump = 2;
private int currentJump = 0;
private Vector3 dashDirection;
[SerializeField] private bool onGround;
public float dashSpeed;
public bool isDashing = true;
// Start is called before the first frame update
void Start()
{
playerRb = GetComponent<Rigidbody>();
isDashing = true;
}
// Update is called once per frame
void Update()
{
jump();
if(isDashing)
{
Dashing();
}
}
void FixedUpdate()
{
movement = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
move(movement);
}
void OnCollisionEnter(Collision collision)
{
onGround = true;
currentJump = 0;
isDashing = true;
}
void jump()
{
if(Input.GetKeyDown("space") && (onGround || maxJump > currentJump))
{
playerRb.velocity = Vector3.up * jumpForce;
onGround = false;
currentJump++;
}
}
void move(Vector3 direction)
{
playerRb.MovePosition((Vector3)transform.position + (direction * speed * Time.deltaTime));
}
void Dashing()
{
if(Input.GetKeyDown(KeyCode.LeftShift) && isDashing == true)
{
playerRb.velocity = transform.right * dashSpeed;
isDashing = false;
}
}
}
Any help is appreciated. Thank you.
Your answer
Follow this Question
Related Questions
Speed boost button for player 0 Answers
i need help making a dash ability for my endless runner game 2 Answers
How to make a movement for 2.5d game? 0 Answers
Make a dash system using the Character Controller of Unity 0 Answers
How do I get my character to walk down / up stairs WITHOUT the use of Rigidbody? 0 Answers