- Home /
Raycasting Distance and info with static variables
In a first-person shooter, I'm trying to determine if the current object 'under' my cross-hair is in "range". I'm using the "ViewportPointToRay" script with "Raycast.distance". It works great but my performance suffers greatly loosing ruffly 400-700fps.
The raycast and distance detection is in one script that sets the distance and contact (true/false) variables as static (same issue even with one static variable). The weapon's script checks these variables also in an Update() and affects the object as required (ie: change its color while raycast is touching it, move it... ect.)
Raycast's Script:
static var TargCont : boolean = false;
static var ObjctInRnge;
static var TargRange;
function Update(){
var ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
ObjctInRnge = hit.transform; //this is the target being touched
TargCont = true; //there is a target being touched
TargRange = hit.distance; //target's range
} else {
TargCont = false;
}
}
The problem is, that I don't know how to go about using the raycast's information in another script's Update() with out sacrificing a great amount of performance.
What should I do?
Thanks a ton!
Answer by Jaap Kreijkamp · Feb 16, 2010 at 01:48 AM
Basically, you're almost there! First, give the script a name starting with uppercase, no spaces and followed by upper/lowercase letters or numbers, let's assume you call it CrosshairCheck.js, as the file name will be the class name. Next attach the script to only one game object (something like GameManager but can be anything of course, an object just to get the update called). From other scripts use it as CrosshairCheck.TargCont
, CrosshairCheck.TargRange
, ... .
Your answer
Follow this Question
Related Questions
Raycasting with particles: How can I do that?? 2 Answers
What does Physics2D.Raycast return? 0 Answers
raycasting in c# - NullRefrenceException error 2 Answers