- Home /
Question About Ray casting in an Area and Perspective
Hi,
I am making the code for tracking on my missile launcher (not the missile but the target). The target is not acquired instantly instead the player must hover the scope over the target for a couple of seconds until it is acquired then fire. However if the target moves out of the area of the cross hair then the timer is reset.
I haven't started coding yet but realise the two biggest problems are working out when the target leaves the area of the scope (considering that the area occupied by the scope is much greater at the targets location due to perspective) using raycasting in an area which I have not done before.
Second problem that I reckon I can solve is making a 2D texture appear over the target which will use Camera.WorldToScreenPoint() to convert the location of the target into a screen location.
I need help with the first problem. How can I ray cast in an area and work out the size of that area at the target?
Thanks
Answer by robertbu · Mar 21, 2013 at 03:22 PM
How difficult the problem is depends on how precise you want to be and how you define 'within the cross hair'. For something simple like making sure the center position of the target stays within a circular area, Simply use WorldToScreenPoint() and compare the distance of the screen center position of cross hair with position of the target. If it is below some radius, then it is within the crosshair. By converting to screen coordinates, you've already projected the 3D world position onto a 2D plane. If you want to make sure that the entire target stays within the cross hair, then you will have to compare the cross hair screen position with the eight bounding box positions of Renderer.bounds. Since bounds will likely include a bit more than the actual target, this is not perfect.
Answer by mxmcharbonneau · Mar 21, 2013 at 03:28 PM
You should probably find the angle between the line from the launcher to the target and the direction forward of your launcher. Something like
Vector3 direction=target.position-launcher.position;
float angle=Vector3.Angle(launcher.forward,direction);
then you check if the angle is in the range you want. Far easier than raycasting to a plane with a variable size.
This was my finished code, thanks for everyones help:
//RocketLauncer.js
function Track()
{
if (CheckTracking())
{
var trackTexture : TrackingIcon = GameObject.Find("TrackingIcon").GetComponent(TrackingIcon);
trackTexture.Track(target);
if (trackTime < 6)
{
trackTime += Time.deltaTime;
trackTexture.blink = true;
if (trackTime < 2.0)
{
trackTexture.blinkSpeed = 1.0;
}
else if (trackTime < 4.0 && trackTime > 2.0)
{
trackTexture.blinkSpeed = 0.7;
}
else if (trackTime < 6.0 && trackTime > 4.0)
{
trackTexture.blinkSpeed = 0.3;
}
}
else
{
trackTexture.blink = false;
canFire = true;
}
}
else
{
ResetTracking();
}
}
function AquireTarget()
{
var ray = Camera.main.ScreenPointToRay(Vector3(Camera.main.pixelWidth/2, Camera.main.pixelHeight/2, 0));
var rayHit : RaycastHit;
if (Physics.Raycast(ray, rayHit))
{
if (rayHit.collider.gameObject.tag == "Target")
{
target = rayHit.collider.gameObject.transform;
tracking = true;
GameObject.Find("TrackingIcon").GetComponent(TrackingIcon).Target = target;
GameObject.Find("TrackingIcon").GetComponent(TrackingIcon).Enable();
}
}
}
function CheckTracking() : boolean
{
var targetPos = Camera.main.WorldToScreenPoint(target.position);
var targetPos2D : Vector2;
targetPos2D = new Vector2(targetPos.x, targetPos.y);
var screenCentre : Vector2;
screenCentre = new Vector2(Camera.main.pixelWidth/2, Camera.main.pixelHeight/2);
var distance : double;
distance = Vector2.Distance(screenCentre, targetPos2D);
if (distance > 140.0)
{
return false;
}
else
return true;
}
function ResetTracking()
{
tracking = false;
canFire = false;
GameObject.Find("TrackingIcon").GetComponent(TrackingIcon).Disable();
trackTime = 0;
}
//TrackingTexture.js
var Target : Transform;
var enabledTracking : boolean = false;
var blink: boolean = false;
var blinkTimer : double = 0;
var blinkSpeed : double = 0.7;
function Start () {
}
function Update ()
{
if (enabledTracking)
{
if (blink)
{
blinkTimer += Time.deltaTime;
if (blinkTimer >= blinkSpeed)
{
blinkTimer = 0;
if (guiTexture.enabled == true)
guiTexture.enabled = false;
else
guiTexture.enabled = true;
}
}
else
guiTexture.enabled = true;
}
}
function Track(target: Transform)
{
var screenPos : Vector3;
screenPos = Camera.main.WorldToViewportPoint(target.position);
transform.position = screenPos;
}
function Enable()
{
guiTexture.enabled = true;
enabledTracking = true;
}
function Disable()
{
guiTexture.enabled = false;
enabledTracking = false;
}
Your answer
Follow this Question
Related Questions
Raycast Decals,Sound and MuzzleFlash not working too well 0 Answers
bullet hole problem/raycast problem || help me please 1 Answer
How to use raycast when standing still. 1 Answer
Raycast Not Rotating 1 Answer
Raycasr in my fps? 1 Answer