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

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » FREE Project Downloads & Recources » Scripts » CrossHair C# version of the script

CrossHair C# version of the script

View previous topic View next topic Go down  Message [Page 1 of 1]

1 CrossHair C# version of the script on Wed Oct 08, 2014 1:43 pm

gothicsyn


Member [Lv. 2]
Member [Lv. 2]
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.

View user profile http://devilsincstudios.com

2 Re: CrossHair C# version of the script on Wed Oct 08, 2014 2:26 pm

NewDeveloper


Moderator
Moderator
Thanks for the share.

--New Developer

View user profile

3 Re: CrossHair C# version of the script on Wed Oct 08, 2014 6:06 pm

Hey, thanks for sharing.

View user profile http://www.youtube.com/user/misterninjaboy

4 Re: CrossHair C# version of the script on Sun Oct 19, 2014 1:43 pm

rocktech


Member [Lv. 2]
Member [Lv. 2]
thanks but it did'nt work

View user profile

5 Re: CrossHair C# version of the script on Tue Oct 21, 2014 9:42 am

gothicsyn


Member [Lv. 2]
Member [Lv. 2]
@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.

View user profile http://devilsincstudios.com

6 Re: CrossHair C# version of the script on Wed Jan 14, 2015 1:49 pm

ItsDaMun14


Member [Lv. 0]
Member [Lv. 0]
help me how do i do this

View user profile

7 Re: CrossHair C# version of the script on Thu Aug 04, 2016 3:07 pm

Pr0totype2


Member [Lv. 3]
Member [Lv. 3]
Why do you use OnGUI to draw the texture?

View user profile

8 Re: CrossHair C# version of the script on Thu Aug 11, 2016 5:00 pm

TNice1997


Member [Lv. 2]
Member [Lv. 2]
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.

View user profile

9 Re: CrossHair C# version of the script on Fri Aug 12, 2016 1:55 pm

Pr0totype2


Member [Lv. 3]
Member [Lv. 3]
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

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum