- Home /
[HELP] Translating vector 2 co-ordinates to real space
I think the title is correct. Tbh i'm not too sure what i need be implementing hence the post.
I am wanting to create a weapon fire pattern using Bitmap images such as this HERE. In which the recoil of the gun will follow the pattern specified. Green is where the first shot will be (the center of the crosshair), then it follows the blue and finally ends on red.I then generate a list of Vector2 which based on the co-ordinates of the pixels in the given image. The output result looks like... X16:Y0, X16:Y1, X15:Y2... and so on.... this part of the code works correctly.
The trouble i am having is getting the crosshair (center of camera) to actually look at the Vector2 co-ordinates.
The following code i am using is:
void LookAtTarget()
{
Vector3 currentView = Camera.main.transform.forward;
Vector3 addView = new Vector3(generateScript.GetVector(point).x, generateScript.GetVector(point).y, 0);
Vector3 newView = currentView + addView;
transform.LookAt(newView);
point++;
Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 100, Color.yellow, 100f);
}
When the gun is fired the above method is called. The theory i had was to simply get what the camera is currently looking at and add the new Vector2. However the results are not what i desired. You can see a GIF of the results HERE. The "point" variable is used to indicate which vector to aim at next from the list
I THINK the issue is related to the 2D/3D conversion, as you can see from the GIF the weapon does not take into consideration the current view (in this example it was straight forward) and instead aims at the 2D vector on the screen space and not world space.
Could anybody offer some guidance or support please? Thank you kindly
Answer by jackmw94 · Apr 08 at 12:06 AM
I think your first problem might be that you’re conflating a look direction with a look target. You pass the look at function the latter whereas you’re using the former. For example if your transform is at (0,0,-2) and you call look at passing (0,0,-1) it will look forward at it’s looking at the position 1 unit in front of it.
It looks as though you want to set its forward direction, you can do this by setting its rotation using Quaternion.LookRotation (https://docs.unity3d.com/ScriptReference/Quaternion.LookRotation.html)
transform.rotation = Quaternion.LookRotation(newView, Vector3.up);
Let me know how you get on with this :)