Unity2d worldspace / local space vector 3 help needed
Hey guys,
im pretty new to programming and for my first game im trying to make a character move with wasd and have it look at the mouse. This is all looking correctly, now i want to try and make a sort of flashlight effect using raycasts into the direction of the mouse. However im running into some problems
//variables for rotation
var mousePosition = Input.mousePosition;
var screenPoint = Camera.main.WorldToScreenPoint(transform.localPosition);
var offset = new Vector2(mousePosition.x - screenPoint.x, mousePosition.y - screenPoint.y);
var angle = Mathf.Atan2(offset.y, offset.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(0, 0, angle);
//make a cone of 30 Degrees
for (int i = 0; i < 30; i = i + 2)
{
//calculating how much ofset there is from the middle line
var rad = i * Mathf.PI / 180;
difference = (Mathf.Tan(rad) * 5);
//upper part of the flashlight
Vector3 directionUp = new Vector3((transform.position.x + 5.0f), transform.position.y + difference, 0.0f);
// directionUp = transform.TransformDirection(directionUp);
//lowerpart of the flashlight
Vector3 directionDown = new Vector3(transform.position.x + 5, transform.position.y - difference, 0.0f);
// directionDown = transform.TransformDirection(directionDown);
Debug.DrawLine(transform.position, directionUp, Color.green);
Debug.DrawLine(transform.position, directionDown, Color.green);
Debug.DrawLine(transform.position, transform.up, Color.blue);
}
Right now im stull visuallising the raycasts using the debug drawline, so no actual raycasting is implemented yet. However the cone of lines is not facing the mouse, it is always facing the right side. I think it has something to do with local space. I think the + 5 on the transform.x is not going on the objects x-axis but on the main x-axis. I tried to do something with transform.direction but couldnt quite work it out as it just bugs out completely.
Would love to get some tips on how to fix this!