Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Share
Go down
Member [Lv. 2]
Member [Lv. 2]
Posts : 9
Points : 1428
Rep : 1
Join date : 2014-10-08
Age : 35
Location : Leeds, UK
View user profilehttp://devilsincstudios.com

CrossHair C# version of the script

on Wed Oct 08, 2014 1:43 pm
Message reputation : 100% (1 vote)
Hey Guys,

I was using Unity Ninjas tutorials to help iron out some issues with my FPS shooter, the problem is I am a C# coder and so had to convert the JS to C# (also the js script kicked up a ton of errors on Unity 4.5 & 4.6)

Code:
using UnityEngine;
using System.Collections;

public class CrossHair : MonoBehaviour {
   
   public Texture crosshair;

   public int offsetw;
   public int offseth;
   public int divide = 1;

   
   void OnGUI() {
      GUI.DrawTexture(new Rect(Screen.width / 2 - offsetw, Screen.height / 2 - offseth, crosshair.width / divide, crosshair.height / divide), crosshair);
   }
}

It is tested and working just fine, the offsetw & h values will adjust the on screen width values at runtime much easier to position it that way. Just remember them then plug them back in after your stop Unity running the divide value will divide the texture size making it smaller.

Hope this helps those C# coders like me.
avatar
Moderator
Moderator
Posts : 194
Points : 2021
Rep : 44
Join date : 2013-12-30
View user profile

Re: CrossHair C# version of the script

on Wed Oct 08, 2014 2:26 pm
Thanks for the share.

--New Developer
avatar
Admin
Admin
Posts : 608
Points : 3197
Rep : 406
Join date : 2013-12-11
Age : 21
View user profilehttps://www.youtube.com/user/misterninjaboy

Re: CrossHair C# version of the script

on Wed Oct 08, 2014 6:06 pm
Hey, thanks for sharing.
Member [Lv. 2]
Member [Lv. 2]
Posts : 5
Points : 1424
Rep : 0
Join date : 2014-10-17
View user profile

Re: CrossHair C# version of the script

on Sun Oct 19, 2014 1:43 pm
thanks but it did'nt work
Member [Lv. 2]
Member [Lv. 2]
Posts : 9
Points : 1428
Rep : 1
Join date : 2014-10-08
Age : 35
Location : Leeds, UK
View user profilehttp://devilsincstudios.com

Re: CrossHair C# version of the script

on Tue Oct 21, 2014 9:42 am
@Rocktech I can quite safely assure you it works it's the same script I use make sure the texture2D and variables in the inspector are set correctly

I have mine attached directly to the gun model which is then parented back to the FPS controllers camera.



I suspect it will have direct repercussion as to where the script is attached as the game will run the OnGUI function once the game is running. Also sorry it took so long to reply on this, been offline with system troubles.
Member [Lv. 0]
Member [Lv. 0]
Posts : 1
Points : 1313
Rep : 0
Join date : 2015-01-14
View user profile

Re: CrossHair C# version of the script

on Wed Jan 14, 2015 1:49 pm
help me how do i do this
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 16
Points : 1324
Rep : 1
Join date : 2015-01-30
Location : new Location(Drones.PR0TOTYPE2)
View user profile

Re: CrossHair C# version of the script

on Thu Aug 04, 2016 3:07 pm
Why do you use OnGUI to draw the texture?
Member [Lv. 2]
Member [Lv. 2]
Posts : 8
Points : 788
Rep : 0
Join date : 2016-07-31
View user profile

Re: CrossHair C# version of the script

on Thu Aug 11, 2016 5:00 pm
Quick question. What is the point of the divide variable? Since you have it set to 1 when the division happens it is just the same as the other number.
avatar
Member [Lv. 3]
Member [Lv. 3]
Posts : 16
Points : 1324
Rep : 1
Join date : 2015-01-30
Location : new Location(Drones.PR0TOTYPE2)
View user profile

Re: CrossHair C# version of the script

on Fri Aug 12, 2016 1:55 pm
TNice1997 wrote:Quick question. What is the point of the divide variable? Since you have it set to 1 when the division happens it is just the same as the other number.
Not only that, but the OnGUI method shouldn't be used to create things
Back to top
Permissions in this forum:
You cannot reply to topics in this forum