- Home /
Using CrossHair GUI to Determine Ray Spread
I'm using the following script to draw a crosshair on the screen that spreads further apart when the player holds the Fire1 button down. The script is as follows:
pragma strict
var drawCrosshair = true;
var crosshairColor = Color.white;
var width : float = 3;
var height : float = 35;
class spreading{
var spread = 20.0;
var maxSpread = 60.0;
var minSpread = 20.0;
var spreadPerSecond = 30.0;
var decreasePerSecond = 5.0;
var maxShrink = 5;
var minShrink = 0;
}
var spread : spreading;
private var tex : Texture2D;
private var lineStyle : GUIStyle;
function Awake (){
tex = Texture2D(1,1);
SetColor(tex, crosshairColor);
lineStyle = GUIStyle();
lineStyle.normal.background = tex;
}
function Update (){
if(Input.GetButton("Fire1")){
spread.spread += spread.spreadPerSecond * Time.deltaTime;
Fire();
}else{
spread.spread -= spread.decreasePerSecond * Time.deltaTime;
}
spread.spread = Mathf.Clamp(spread.spread, spread.minSpread, spread.maxSpread);
if(Input.GetButton("Crouch")){
spread.spread -= spread.spreadPerSecond * Time.deltaTime;
Crouch();
spread.spread = Mathf.Clamp(spread.spread, spread.minShrink, spread.maxShrink);
}
}
function OnGUI (){
var centerPoint = Vector2(Screen.width / 2, Screen.height / 2);
if(drawCrosshair){
GUI.Box(Rect(centerPoint.x - width / 2, centerPoint.y - (height + spread.spread), width, height), "", lineStyle);
GUI.Box(Rect(centerPoint.x - width / 2, centerPoint.y + spread.spread, width, height), "", lineStyle);
GUI.Box(Rect(centerPoint.x + spread.spread, (centerPoint.y - width / 2), height , width), "", lineStyle);
GUI.Box(Rect(centerPoint.x - (height + spread.spread), (centerPoint.y - width / 2), height , width), "", lineStyle);
}
}
function Fire(){
}
function Crouch(){
}
function SetColor(myTexture : Texture2D, myColor : Color){
for (var y : int = 0; y < myTexture.height; ++y){
for (var x : int = 0; x < myTexture.width; ++x){
myTexture.SetPixel(x, y, myColor);
}
}
myTexture.Apply();
}
Is it possible to use the spread variables to determine the area within which the ray casts randomly and is it possible to have the raycasting in a separate script?
P.S I'm still pretty new to coding so sorry if this has a very simple solution.
Comment