- Home /
How to make 2D Player dash towards mouse position in a straight line?
Hi, I'm trying to create a dash similar to the one used in Katana Zero, in which on clicking the mouse button to attack the player does a slight dash in that direction.
// I'm trying to achieve this using AddForce, the code I'm using seems to work but the player seems to move first along the X axis and then up the Y axis towards the mouseclick point. Also, the force added isn't equal at all points, clicking the mouse directly above the player adds a lot more force than when clicking at an angle, and clicking further away from the player adds more force than closer to the player.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DashToMouse : MonoBehaviour
{
[SerializeField] private float dashSpeed;
[SerializeField] private float startDashTime;
private float dashTime;
private Rigidbody2D rd2d;
private Animator anim;
private Camera cam;
// Start is called before the first frame update
void Start()
{
rd2d = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
cam = Camera.main;
}
private void FixedUpdate()
{
DashToPoint();
}
protected void DashToPoint()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0;
Vector3 difference = mousePos - transform.position;
difference = difference.normalized;
float angle = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
Vector3 dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right;
dashTime = startDashTime;
while (dashTime > 0)
{
anim.SetTrigger("ground up swipe");
rd2d.AddForce(dir * dashSpeed);
dashTime = dashTime - Time.deltaTime;
}
}
}
}
Any help appreciated, thanks! :)
Answer by goduster · Nov 10, 2019 at 11:44 AM
Well I can help you a bit cause I got the same problem. But remember that my game is top down
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
float x = Input.GetAxisRaw("Horizontal"); // GetAxisRaw means that when pressed I have 1 or -1immediately
float y = Input.GetAxisRaw("Vertical");
playerPosition = new Vector2(x, y);
if (Input.GetMouseButtonDown(1))
{
DashAbility(x,y);
Debug.Log("Mouse1");
}
Debug.DrawRay(transform.position, mouseDirection, Color.green); // this makes ray(in scene) so I can see where I will teleport
}
void FixedUpdate()
{
rb.velocity = playerPosition * moveSpeed; // this is just my movement
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mouseDirection = mousePosition - transform.position;
}
public void DashAbility(float x, float y) // so here I tried to do dragon(from Katana Zaro) dash but I believe that for attack it would be the same
{
rb.velocity = Vector2.zero;
dir = new Vector2(mousePosition.x, mousePosition.y); // remember that mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = dir; // the only thing to change is this cause you need to move for an equal ammount of units each time you attack
}
transform.position = dir; // the only thing to change is this cause you need to move for an equal ammount of units each time you attack and not teleport
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiated object not showing in scene or hierarchy 2 Answers
,How to stop jump animtion 1 Answer