i wanna make my charactor dash to direction of my mouse
how should i do it
here is my code
please help me:)
using System.Collections;
using UnityEngine;
public class PlayerGrappling : MonoBehaviour
{
private Rigidbody2D _rigidbody2D;
private LineRenderer _lineRenderer;
private Transform _closest;
private SpringJoint2D _springJoint2D;
private Collider2D collision;
private SpriteRenderer spriteRenderer;
private float _maxSpeed = 30f;
public GameObject hitEffect;
public GameObject dieEffect;
public GameObject player;
public GameObject Diepoint;
public CircleCollider2D CCollider;
public float forceAmount;
private void Start()
{
_rigidbody2D = GetComponent<Rigidbody2D>();
_lineRenderer = GetComponent<LineRenderer>();
collision = GetComponent<Collider2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
_lineRenderer.positionCount = 0;
}
private void Update()
{
if (Input.GetMouseButtonDown(1))
{
dash();
}
if (Input.GetMouseButton(0))
{
// draw line from player to hinge
_lineRenderer.SetPosition(0, transform.position);
_lineRenderer.SetPosition(1, _closest.position);
_rigidbody2D.AddForce(transform.forward * forceAmount, ForceMode2D.Impulse);
if (_springJoint2D.distance > 3)
{
_springJoint2D.distance -= 0.01f;
}
}
if (Input.GetMouseButtonUp(0))
{
// remove line and hinge
_lineRenderer.positionCount = 0;
_springJoint2D.connectedBody = null;
}
}
private void FixedUpdate()
{
if (_rigidbody2D.velocity.magnitude > _maxSpeed)
{
_rigidbody2D.velocity = _rigidbody2D.velocity.normalized * _maxSpeed;
}
}
Transform GetClosestHinge(GameObject[] hinges)
{
Transform closest = null;
var closestDistanceSqr = Mathf.Infinity;
var currentPosition = transform.position;
foreach (var potentialTarget in hinges)
{
var directionToTarget = potentialTarget.transform.position - currentPosition;
var dSqrToTarget = directionToTarget.sqrMagnitude;
if (!(dSqrToTarget < closestDistanceSqr)) continue;
closestDistanceSqr = dSqrToTarget;
closest = potentialTarget.transform;
CCollider.enabled = true;
}
return closest;
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "unable")
{
blue();
}
if (collision.gameObject.tag == "Die")
{
Die();
_lineRenderer.positionCount = 0;
_springJoint2D.connectedBody = null;
}
}
void Die()
{
Debug.Log("Die");
Destroy(player);
Instantiate(dieEffect, collision.transform.position, Quaternion.identity);
}
void blue()
{
spriteRenderer.color = new Color(1, 1, 1, 0.4f);
_springJoint2D.enabled = false;
}
void dash() {
}
}
,i wanna add the dash abillity to my player
my player charactor is unable to move because i didnt code it to
so i just wanna make it dash to the mouse pointer’s direction
my code is this
using System.Collections;
using UnityEngine;
public class PlayerGrappling : MonoBehaviour
{
private Rigidbody2D _rigidbody2D;
private LineRenderer _lineRenderer;
private Transform _closest;
private SpringJoint2D _springJoint2D;
private Collider2D collision;
private SpriteRenderer spriteRenderer;
private float _maxSpeed = 30f;
public GameObject hitEffect;
public GameObject dieEffect;
public GameObject player;
public GameObject Diepoint;
public CircleCollider2D CCollider;
public float forceAmount;
private void Start()
{
_rigidbody2D = GetComponent<Rigidbody2D>();
_lineRenderer = GetComponent<LineRenderer>();
collision = GetComponent<Collider2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
_lineRenderer.positionCount = 0;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
// get the closest hinge
_closest = GetClosestHinge(GameObject.FindGameObjectsWithTag("hinge"));
// attach joint to it
_springJoint2D = _closest.GetComponent<SpringJoint2D>();
_springJoint2D.connectedBody = _rigidbody2D;
// set line renderer position count
_lineRenderer.positionCount = 2;
}
if (Input.GetMouseButton(0))
{
// draw line from player to hinge
_lineRenderer.SetPosition(0, transform.position);
_lineRenderer.SetPosition(1, _closest.position);
_rigidbody2D.AddForce(transform.forward * forceAmount, ForceMode2D.Impulse);
if (_springJoint2D.distance > 3)
{
_springJoint2D.distance -= 0.01f;
}
}
if (Input.GetMouseButtonUp(0))
{
// remove line and hinge
_lineRenderer.positionCount = 0;
_springJoint2D.connectedBody = null;
}
}
private void FixedUpdate()
{
if (_rigidbody2D.velocity.magnitude > _maxSpeed)
{
_rigidbody2D.velocity = _rigidbody2D.velocity.normalized * _maxSpeed;
}
}
Transform GetClosestHinge(GameObject[] hinges)
{
Transform closest = null;
var closestDistanceSqr = Mathf.Infinity;
var currentPosition = transform.position;
foreach (var potentialTarget in hinges)
{
var directionToTarget = potentialTarget.transform.position - currentPosition;
var dSqrToTarget = directionToTarget.sqrMagnitude;
if (!(dSqrToTarget < closestDistanceSqr)) continue;
closestDistanceSqr = dSqrToTarget;
closest = potentialTarget.transform;
CCollider.enabled = true;
}
return closest;
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "unable")
{
blue();
}
if (collision.gameObject.tag == "Die")
{
Die();
_lineRenderer.positionCount = 0;
_springJoint2D.connectedBody = null;
}
}
void Die()
{
Debug.Log("Die");
Destroy(player);
Instantiate(dieEffect, collision.transform.position, Quaternion.identity);
}
void blue()
{
spriteRenderer.color = new Color(1, 1, 1, 0.4f);
_springJoint2D.enabled = false;
}
}