- Home /
How Do I Make A Dash&Attack Move For My Top-DownRPG 2D?
I Made This RPG Kinda Game And Unfortunately I Can Make Any Attack Movement. I Want Kind Of Like A Dash Attack That Kills These Slimes. At the moment the slimes just reduce your health on collision and you die if it reaches zero. Also I Have A Crosshair That Is Restricted and cant go far from the player, and I want To Dash&Attack In The Direction Of My Crosshair. I Want To Be Able To Kill Them And Also Detect That I Have Killed The Specific Slime Because I Want The Slime As An Item In Future. Here Is My Movement Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public GameObject player;
public Animator animator;
public float movementSpeed;
public float runSpeed;
public int sprintDuration=150;
int sprintRemaining=100;
public GameObject footstepGrass;
public GameObject footstepGrassSprint;
public GameObject footstepPlanks;
public GameObject footstepPlanksSprint;
public GameObject footstepCave;
public GameObject footstepCaveSprint;
public GameObject caveParticles;
public GameObject groundMaterial;
public Rigidbody2D rb;
Vector3 movement;
Vector3 aim;
bool isAiming;
bool endofAiming;
bool isSprinting;
void Update()
{
ProcessInputs();
Animate();
Move();
Footsteps();
}
private void ProcessInputs()
{
movement = new Vector3(Input.GetAxis("MoveHorizontal"), Input.GetAxis("MoveVertical"), 0.0f);
if(movement.magnitude > 1.0f)
{
movement.Normalize();
}
}
private void Footsteps()
{
if(Input.GetAxis("MoveHorizontal") != 0 && !isSprinting && groundMaterial.tag == "Grass")
{
footstepGrass.SetActive(true);
}
else if(Input.GetAxis("MoveVertical") != 0 && !isSprinting && groundMaterial.tag == "Grass")
{
footstepGrass.SetActive(true);
}
else if(Input.GetAxis("MoveHorizontal") != 0 && isSprinting && groundMaterial.tag == "Grass")
{
footstepGrassSprint.SetActive(true);
}
else if(Input.GetAxis("MoveVertical") != 0 && isSprinting && groundMaterial.tag == "Grass")
{
footstepGrassSprint.SetActive(true);
}
else
{
footstepGrass.SetActive(false);
footstepGrassSprint.SetActive(false);
}
if(Input.GetAxis("MoveHorizontal") != 0 && !isSprinting && groundMaterial.tag == "Planks")
{
footstepPlanks.SetActive(true);
}
else if(Input.GetAxis("MoveVertical") != 0 && !isSprinting && groundMaterial.tag == "Planks")
{
footstepPlanks.SetActive(true);
}
else if(Input.GetAxis("MoveHorizontal") != 0 && isSprinting && groundMaterial.tag == "Planks")
{
footstepPlanksSprint.SetActive(true);
}
else if(Input.GetAxis("MoveVertical") != 0 && isSprinting && groundMaterial.tag == "Planks")
{
footstepPlanksSprint.SetActive(true);
}
else
{
footstepPlanksSprint.SetActive(false);
footstepPlanks.SetActive(false);
}
if(Input.GetAxis("MoveHorizontal") != 0 && !isSprinting && groundMaterial.tag == "Cave")
{
footstepCave.SetActive(true);
}
else if(Input.GetAxis("MoveVertical") != 0 && !isSprinting && groundMaterial.tag == "Cave")
{
footstepCave.SetActive(true);
}
else if(Input.GetAxis("MoveHorizontal") != 0 && isSprinting && groundMaterial.tag == "Cave")
{
footstepCaveSprint.SetActive(true);
}
else if(Input.GetAxis("MoveVertical") != 0 && isSprinting && groundMaterial.tag == "Cave")
{
footstepCaveSprint.SetActive(true);
}
else
{
footstepCaveSprint.SetActive(false);
footstepCave.SetActive(false);
}
}
private void Move()
{
//transform.position = transform.position + movement * Time.deltaTime * movementSpeed;
rb.velocity = new Vector2(movement.x, movement.y);
if(Input.GetAxis("Sprint") != 0 && sprintRemaining > 0)
{
transform.position = transform.position + movement * Time.deltaTime * runSpeed;
sprintRemaining -= 1;
isSprinting = true;
}
else if(Input.GetAxis("Sprint") == 0 && sprintRemaining < sprintDuration)
{
sprintRemaining++;
}
else
{
isSprinting = false;
}
}
private void Animate()
{
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Magnitude", movement.magnitude);
}
}
Here is my CrossHair Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CrossHair : MonoBehaviour
{
public GameObject crossHair;
bool isAiming;
bool endofAiming;
Vector3 aim;
void Awake()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
isAiming = Input.GetButton("Fire1");
endofAiming = Input.GetButtonUp("Fire1");
Vector3 mouseMovement = new Vector3(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"), 0.0f);
aim = aim + mouseMovement;
if(aim.magnitude > 1f)
{
aim.Normalize();
}
Aim();
}
private void Aim()
{
Vector2 shootingDirection = new Vector2(aim.x, aim.y);
if(aim.magnitude > 0.0f)
{
crossHair.transform.localPosition = aim * 0.3f;
}
}
}
PS:- Im Using Three Prefabs For The Slime With Each Different Color. The Slimes Spawn Randomly On The Map. Help Would Be Appreciated.
Your answer
![](https://koobas.hobune.stream/wayback/20220612205956im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D Dash attack 0 Answers
How do I set direction of player dash movement? Currently dashes to exact mouse position. 2 Answers
I need help fixing this code 1 Answer
2d Top Down Shooter Jerky Movement 0 Answers
unity 2d platformer dash attack. 1 Answer