FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.

Share

descriptionCrossHair C# version of the script

more_horiz
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.

descriptionRe: CrossHair C# version of the script

more_horiz
Thanks for the share.

--New Developer

_________________
Serious, Focused but FUN.

descriptionRe: CrossHair C# version of the script

more_horiz
Hey, thanks for sharing.

descriptionRe: CrossHair C# version of the script

more_horiz
thanks but it did'nt work

descriptionRe: CrossHair C# version of the script

more_horiz
@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.

descriptionRe: CrossHair C# version of the script

more_horiz
help me how do i do this

descriptionRe: CrossHair C# version of the script

more_horiz
Why do you use OnGUI to draw the texture?

descriptionRe: CrossHair C# version of the script

more_horiz
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.

descriptionRe: CrossHair C# version of the script

more_horiz
[You must be registered and logged in to see this link.] 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
Permissions in this forum:
You cannot reply to topics in this forum