A few weeks back I posted a question about keeping an object on the screen. What I would like to do now is clamping an object around a 2d radius based on the camera.
I'm need this for a reticule for this on rail shooter I've been making. (like that of starfox)
Here is the current code I am using :
Vector3 movementx = transform.TransformDirection(Vector3.right * Input.GetAxis("Mouse X") * aimSpeed);
Vector3 newPosx = transform.position + movementx;
Vector3 offsetx = newPosx - centerPt.position;
transform.position = centerPt.position + Vector3.ClampMagnitude(offsetx, radius);
// the same for the y axis
The problem with this set up is that I'm restricting it around 3-dimensional objects base off their locations in the world. (this is restricting the players movement towards the edges not letting the player to move the reticule to the side)
So this is what I've tried:
Vector3 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
Vector3 shipOnScreen = Camera.main.WorldToScreenPoint(Ship.position);
Vector3 movementx = transform.TransformDirection(Vector3.right * Input.GetAxis("Mouse X") * aimSpeed);
Vector3 newPosx = screenPosition + movementx;
Vector3 offsetx = newPosx - shipOnScreen;
transform.position = shipOnScreen + Vector3.ClampMagnitude(offsetx, radius);
// the same for the y axis
This just seems to send the empty (which the ship is pointing at) off some where else in the world I cant even tell if I'm moving it or if its just moving with the parent object. (I've tried different variations with this moving the screenPosition swapping it with the transforms position in world space to no avail)
My guess is that its not converting 2d space to 3d space when I try to clamp a 3d object within that radius.
I would like to keep my reticule at an empty point on the screen and not replace it with the cursor.
Edit:
Okay i have now thought up a new system that still needs some work. Having an empty object next to the reticule. With this I cut out the need for a 2d radius but I need this object object follow the ship on on the screen with taking in account the perception of the camera.
I have taken a measure of the height and width of the camera. Its not too exact but it should do.
So i guess I would like to know how the coordinate system on the camera is set up. like where is (0, 0)
update:
Okay I've found a way to do this but it took a quite a bit of math on my part and a bit of estimation on my behalf.
The way I've set it up is that I moving the pivot the same way I'm moving the ship only I'm multiplying it times the proportion of distance the ship can move side to side vs the distance the pivot can move side to side. I still have not built in an equation that will allow me to move the point farther back with more accuracy but this is a good temporary fix and if i really have to move it I'll i do the math again. but I would still like to have the equation of the rate of change. I'm not that brilliant at related rates. If you want to see my math I noted it down.
but if i can have an equation out of this it would work perfectly while changing
ship distance (constant) (61.6209, 27.5131)
first measure distance : 100
point A distance (320.5, 143.1)
5.20115872075484 y-multiplier
5.200473802917458 x-multiplier
so the multiplier at 100
5.2
distance : 50
point B distance (192.0436, 85.74551)
3.11633 y-multiplier
3.11633 x-multiplier
so the multiplier at 100
3.11633
Rate of change (5.2-3.1165)/(100-50) = ROC
ROC = 0.04166934
mt = 0.042
y = No idea
x = distances from ship
=0.042
Oh in case your wondering how i got my measurements I measured the distance manually with points.