I’m working on an First Person Shooter and I was searching how to make a smart crosshair and I didn’t found anything, I explain (what is my definition of smart crosshair) :
That become bigger when you shoot(not to much, just a touch)
Depending where you focus is size change
And it is basically this. I finally want how you can make it : with a script, raycast… And maybe some explanation or the final script (if is the good way to make it).
//Slap this script on to the main camera or similar and you’ll have a GUI driven crosshair
#pragma strict
var drawCrosshair = true;
var crosshairColor = Color.white; //The crosshair color
var width : float = 3; //Crosshair width
var height : float = 35; //Crosshair height
class spreading{
var spread = 20.0; //Adjust this for a bigger or smaller crosshair
var maxSpread = 60.0;
var minSpread = 20.0;
var spreadPerSecond = 30.0;
var decreasePerSecond = 25.0;
}
var spread : spreading;
private var tex : Texture2D;
private var lineStyle : GUIStyle;
function Awake (){
tex = Texture2D(1,1);
SetColor(tex, crosshairColor); //Set color
lineStyle = GUIStyle();
lineStyle.normal.background = tex;
}
function Update (){
if(Input.GetButton("Fire1")){
spread.spread += spread.spreadPerSecond * Time.deltaTime; //Incremente the spread
Fire();
}else{
spread.spread -= spread.decreasePerSecond * Time.deltaTime; //Decrement the spread
}
spread.spread = Mathf.Clamp(spread.spread, spread.minSpread, spread.maxSpread);
}
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(){
//Carry out your normal shooting and stuff
}
//Applies color to the crosshair
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();
}
I spend a little bit of time on this and I managed to create the script above. The script is rather basic and there is a lot that you can do with it, but I guess it will do, for now at least.
Hello, First Thank You. Second, I tweak a bit with your script and it's really good to make the crosshair. Now do you have any idea to make it focus ? To make it bigger for when it shoot I think about a function that basically said : Add 10 to spread, but I'm not sure if it will be smooth so...
I don't really get what you mean by "focus". Do you mean aim down sights or not? I edited the example to spread when you hold down right mouse button. Rather basic example, but it will get you going.
if shoote, replace one bigger texture when press fire button,you can adjust the size by GUI.DrawTexture function.
use Raycast to gernerate one ray from fire position and if it is hit something with tag,for example “Enemy”, replace the texture that belongs to “Enemy”(you need assign tag “Enemy” to your Enemy object.)
Thank You for your answer, but I already figure it out. My problem is really when he shoot and he focus. Is your technique is good when he shooting (smooth).
How does the code not do what it is supposed to? In the long run? It is an example of a crosshair. The specific setup is irrelevant as it could be done in countless ways. And no offence taken - yet your provided code seems to add nothing but less of what the OP asked. And just to be picky your lineWidth is applied incorrectly - it does not center as it should. And yes. Having incremental, decremental operations and alike in OnGUI might not be very advisable, yet it fulfills its purpose. :)
@Wiseman17 If you want someone to be notified of a comment reply you have to begin the comment with @theirNameAccuratelySpelledHere. Additionally, in my experience you can't tag more than one person in a comment, so if you want me to be notified as well as OrangeLightning, you need to do two comments. Just FYI :)
You can always have 2 images of a crosshair, one normal and one spread out. use Gameobject.SetActive while moving/shooting etc. I’m doing it in my games at least.
i dont know how to make that kind of crosshairs but what you can do is add the cross hairs in the game there is a crosshairs script in the game and the texture is the aim one i hope that helps
Any idea , please ?
– NhommeBeurreOneAnswering your own question like this won't help. If you want to 'bump' just edit your question a little and save
– DaveATake this in note
– NhommeBeurreOne... Then work on simpler things, until you reach the level at which you do understand it.
– syclamothAs syclamoth said you would never want to bite of more than you can chew. You might end up choking ;).
– FLASHDENMARK