- Home /
Lock On Reticle
Hi Guys,
I would like to place a reticle over a targeted object on the screen from a camera script. At the moment I have got a target object to which the player and camera look and orbit around on a button press, controlled by a separate script 'Target_Control'. I have previously managed to get the target to the centre of the screen, but this is not the desired effect. As such, here is the specific code from the script I've managed to write so far, though this displays nothing. I know that all the variables are valid, so I believe my process is wrong.
Any help would be very much appreciated!
Cheers,
Popuppirate
using //etc, etc.
public GameObject local_target;
public Texture2D crosshair;
void OnGUI(){
Target_Control targetcontrol = GetComponentInParent <Target_Control> ();
if (targetcontrol.targeting_on == true) {
Vector3 screen_pos = this.camera.WorldToScreenPoint (local_target.transform.position);
GUI.DrawTexture (new Rect (screen_pos.x, screen_pos.y, crosshair.width, crosshair.height), crosshair);
}
}
}`
Answer by popuppirate · Oct 20, 2014 at 09:34 PM
This is the code I came up with, which takes two booleans, targeting_on and zoom_on. When targeting_on=true, the local target is set as the reticle point. when it is false, no target is selected. When the zoom button is pressed, the reticle appears out in front of the object using a raycast to find where the target hit out in front and moving the reticle there.
Unfortunately, I can't work out what to do with if the hit returns false; I tried placing an invisible object to target out in front for this purpose but this had very odd side effects (reticle appearing at corners of screen, for example).
Cheers,
Popuppirate
void OnGUI(){
if(targeting_on==true){
reticle_pos= this.camera.WorldToScreenPoint(local_target.transform.position);
GUI.DrawTexture(new Rect((reticle_pos.x-(reticle_size/2f)),(Screen.height-(reticle_pos.y+(reticle_size/2f))), reticle_size,reticle_size), reticle);
}
if(zoom_on==true){
RaycastHit hit;
bool in_front=Physics.Raycast(player.transform.position, player.transform.forward, out hit);
if(in_front==true){
reticle_pos = this.camera.WorldToScreenPoint(hit.point);
GUI.DrawTexture(new Rect((reticle_pos.x-(reticle_size/2f)),(Screen.height-(reticle_pos.y+(reticle_size/2f))), reticle_size,reticle_size), reticle);
}
}
}
}
Your answer
Follow this Question
Related Questions
Show a target above the player 0 Answers
Moving Camera With 2 Players 3 Answers
WorldToViewportPoint and WorldToScreenPoint give wrong positions when VR is enabled 1 Answer
Target Box not bounding targets 1 Answer
How can I accurately convert a Game Object's position to a sub Canvas/Rect screen position? 0 Answers