I am using Mouse.current.position.ReadValue() to cast a ray from the player to the current mouse postion in order to make the player dash in that direction.
If i move the player (change the players position), there seems to be an offset of my mouse position relative to how far my player is away from 0.0 world coordinates.
7zxh11
The Script:
using UnityEngine;
using UnityEngine.InputSystem;
namespace Entity.Player
{
public class PlayerMovementDash : MonoBehaviour
{
[SerializeField][Tooltip("Dash distance of the player")]
private float dashDistance = 500f;
[SerializeField][Tooltip("Dash InputAction of the player")]
private InputAction dash;
[SerializeField][Tooltip("Main Camera Object")]
private Camera mainCam;
private CharacterController _controller;
private void OnEnable()
{
dash.Enable();
}
private void OnDisable()
{
dash.Disable();
}
void Start()
{
_controller = GetComponent<CharacterController>();
}
void Update()
{
Vector2 mousePosition = mainCam.ScreenToWorldPoint(Mouse.current.position.ReadValue());
Vector2 dashDirection = new Ray(transform.position,mousePosition).direction;
if (dash.triggered) _controller.Move(dashDirection * (Time.deltaTime * dashDistance));
Debug.DrawRay(transform.position,mousePosition, Color.red, 2);
}
}
}