- Home /
Question about a space shooter crosshair targeting script
I've been trying to figure out how the following game's mouse targeting mechanics work: http://www.matteley.co.uk/labs/unity3d-spacerunner/
I managed to find a similar script on unityAnswers, but it doesn't have the same feel Space Runner provides where you're pretty much pulling on the crosshair, or at least that's how I interpret the feel.
Anybody have any idea how this can be accomplished?
Actually, here's a great tutorial on how to do what I'm looking for. Just fix up the code to your needs. Enjoy people, guy deserves at least a like.
http://www.youtube.com/watch?v=39vaHGpwrRI&list=PL758D41DE629781C4∈dex=64&feature=plpp_video
Answer by Jacek_Wesolowski · May 06, 2012 at 02:52 PM
It looks like there are four objects placed in 3D space in front of the camera. Each can only move on a plane that's always parallel to the sceen. Also, each of the four planes is in a different, but constant distance from the camera.
The four objects are: the spaceship, the green rectangle, the crosshair, and an invisible "cursor".
The cursor moves around freely, just like an ordinary mouse cursor. It is, in fact, the mouse cursor, only translated into 3D space. It has no delay or drag, but you can't see that, because the cursor remains invisible.
The ship follows the cursor with a delay. You can create this kind of effect using the Lerp()
function. The other two objects are always set to stay on an invisible line connecting the middle of the ship and the "cursor". Also, all three visible objects are always facing the "cursor". This can be achieved using the LookAt()
function.
The hard part is moving an object around without allowing it to leave its designated plane. The simplest (but also quite limiting) way to do this is to assume the camera is moving along one of global axes - say, the z axis. Then all you need to do after lerping the ship and the crosshair is to reset their transform.position.z
to some constant value of your choice, relative to camera's own transform.position.z
.
$$anonymous$$aybe this is what I'm looking for:
http://answers.unity3d.com/questions/185346/rotate-object-to-face-mouse-cursor-for-xz-based-to.html
Having a little trouble understanding it though.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071138im_/https://answers.unity.com/themes/thub/images/avi.jpg)