- Home /
.normalize doesn't seem to be working properly or something, idk im tired and annoyed as frick
Ok, so this is the line of code that's been making me want to bash my head into the wall so I won't wake up for a week.
destination = transform.position + ((Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized * dashDistance);
This measily line of code should give me a point exactly dashDistance distance away from my player. However, it's not doing that.
The normalized direction vector that gets multiplied by the dash distance and then added to the player position to give the destination point should have absolutely 0 wiggle room for how far the destination point is away from the player. It should always be dashDistance. But it's not and I've been pulling my hair out as to why.
I'm also adding the script just to give the needed context.
public class Player_Movement : MonoBehaviour
{
public Rigidbody2D rb2d;
public float dashDistance = 5f;
public float dashSpeed = 50f;
public Vector2 destination;
public Vector2 direction;
private bool dashing;
void Start()
{
rb2d = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Fire1") != 0)
{
}
if (Input.GetKeyDown(KeyCode.Mouse1))
{
destination = transform.position + ((Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position).normalized * dashDistance);
direction = (destination - new Vector2(transform.position.x, transform.position.z)).normalized;
dashing = true;
}
if (dashing)
{
//rb2d.velocity = direction * dashSpeed;
if (Vector2.Dot(direction, (destination - new Vector2(transform.position.x, transform.position.z)).normalized) < 0.6f)
{
Debug.Log("gap too big");
rb2d.velocity = Vector2.zero;
dashing = false;
}
}
}
private void OnDrawGizmos()
{
Gizmos.DrawSphere(destination, 0.5f);
//Gizmos.DrawLine(transform.position, destination);
Gizmos.DrawRay(transform.position, direction);
Gizmos.DrawRay(transform.position, (destination - new Vector2(transform.position.x, transform.position.z)).normalized);
}
}
This is the result of the gizmos and I just don't get it. The blue circle is the player, the grey circle is the destination, and the white line is the "direction to destination"
WHY CAN I PLACE MY DESTINATION SO CLOSE TO MY PLAYER, IT SHOULDN'T BE WORKING LIKE THAT
WHY DOES THE LINE NOT POINT TO THE DESTINATION DESPITE, at least for what I know, THE CODE TELLING IT TO DO SO
pls help, im either missing a stupid little mistake or this has to be some issue with .normalized...