I been struggling with getting a Reticle to position itself were a object is Facing. The object is being controlled (spaceship) and i tried casting a Ray infront of it and translating the end point of the ray to Screen Pixels and having the reticle position itself when the ship moves rather then just sitting center of screen.
Can anyone offer me ideas how to get this to work.
The camera follows behind the ship but, when the ship moves left to right the camera plays a little bit of catchup so i want the reticle to move and stay infront of the ship about 10 units.
This is my main hud script iv put together… Almost got it working but, whats happening now is the reticle is fine left to right… but it seems like the y axis is upside down? when i move the ship up the reticle goes down… any idea how to fix this?
var cameraHeight : float = CameraFollow.height;
var cameraDistance : float = CameraFollow.distanceToFollow;
var cameraMoveSpeed : float = CameraFollow.moveSpeed;
var target : Transform;
var reticleTexture:Texture2D;
var reticleTextureOverEnemy:Texture2D;
private var currentTexture:Texture2D;
var minSize:float = 50;
var maxSize:float = 100;
var maxDistance:float = 200;
var visible:boolean = true;
var reticleDistance:float = 10;
private var hit:RaycastHit;
private var rectangle:Rect;
private var screenHeight:int;
private var screenWidth:int;
private var targetRay : Ray;
function Start()
{
screenHeight = Screen.height;
screenWidth = Screen.width;
rectangle = new Rect(screenWidth/2,screenWidth/2,minSize,minSize);//Sets draw location for reticle
}
function Update()
{
reticleRay = new Ray(target.position,target.forward);
point = reticleRay.GetPoint(reticleDistance);
//point = Vector3.Slerp(transform.position, point,Time.deltaTime);
//Convert Ray Point to Screen Pixels
var reticlePos : Vector3 = camera.WorldToScreenPoint (point);
Debug.Log (reticlePos);
screenWidth = Screen.width;
screenHeight = Screen.height;
//var ray:Ray = Camera.main.ScreenPointToRay(new Vector3(reticlePos.x,ReticlePos.y)); //Checks center of screen for raycast.
if (Physics.Raycast(reticleRay, hit, maxDistance)) //Runs check to see if there is a raycast collision within our maximum distance.
{
//Collider for Reticles to Change
if(hit.collider.tag == "Enemy")
currentTexture = reticleTextureOverEnemy;
}
else
{
currentTexture = reticleTexture;
}
//this is a maping system to resize values to a different size. 5 out 10 would be resized to 50 out of 100 if it were map(5, 0, 10, 0, 100);
rectangle.width = rectangle.height = Map(hit.distance, 0, maxDistance, maxSize, minSize);
//Without the above line, our reticle wouldn't resize hardly ever, and it also only scales the size between are max and min value. Max is first otherwise the reticle would get larger with the further distance.
//Clamp for Reticle Size
if(rectangle.width < minSize)
{
rectangle.width = rectangle.height = minSize;
}
if(rectangle.width > maxSize)
{
rectangle.width = rectangle.height = maxSize;
}
else
{
//No object found from maximum distance draw smallest size
rectangle.width = rectangle.height = minSize;
currentTexture = reticleTexture;
}
//reticlePos.y += reticlePos.y;
rectangle.x = reticlePos.x - rectangle.width/2;
rectangle.y = reticlePos.y - rectangle.height/2;
}
function OnGUI () {
//Ship Speed
GUI.Box(Rect(Screen.width - 100,10,100,25),"Speed: "+ shipController.shipSpeed.ToString("f2"));
//Reticle
if(visible)
{
GUI.DrawTexture(rectangle, currentTexture);
}
}
function Map(value:float, fromLow:float, fromHigh:float, toLow:float, toHigh:float)
{
return toLow + (toHigh - toLow) * ((value - fromLow) / (fromHigh - fromLow));
}