I just purchased a script from online and i didn’t realize it was in JS. Any one willing to translate this for me into C#? I’ve tried but keep on failing
var Point : float;
private var GetHitEffect : float;
public var targY : float;
private var PointPosition : Vector3;
var PointSkin : GUISkin;
var PointSkinShadow : GUISkin;
function Start() {
Point = Mathf.Round(Random.Range(Point/2,Point*2));
PointPosition = transform.position + Vector3(Random.Range(-1,1),0,Random.Range(-1,1));
targY = Screen.height /2;
}
function OnGUI() {
var screenPos2 : Vector3 = Camera.main.camera.WorldToScreenPoint (PointPosition);
GetHitEffect += Time.deltaTime*30;
GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7);
GUI.skin = PointSkinShadow;
GUI.Label (Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString());
GUI.skin = PointSkin;
GUI.Label (Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString());
}
function Update() {
targY -= Time.deltaTime*200;
}
Basically, in C# you needed to place a new keyword before struct creators like Vector3(…) and Rect(…):
public float Point;
private float GetHitEffect;
public float targY;
private Vector3 PointPosition;
public GUISkin PointSkin;
public GUISkin PointSkinShadow ;
void Start() {
Point = Mathf.Round(Random.Range(Point/2,Point*2));
// C# requires the "new" keyword before any structure
PointPosition = transform.position + new Vector3(Random.Range(-1,1),0,Random.Range(-1,1));
targY = Screen.height /2;
}
void OnGUI() {
// Camera.main IS a camera reference! No need for .camera
Vector3 screenPos2 = Camera.main.WorldToScreenPoint (PointPosition);
// this instruction makes no sense in OnGUI!
// maybe it should be moved to Update
GetHitEffect += Time.deltaTime*30;
GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7);
GUI.skin = PointSkinShadow;
// Rect also needs a "new" keyword in C#
GUI.Label (new Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString());
GUI.skin = PointSkin;
GUI.Label (new Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString());
}
void Update() {
targY -= Time.deltaTime*200;
}
NOTE: As said in the comments, there’s a weird instruction in OnGUI that adds Time.deltaTime*30 to GetHitEffect - this makes no sense, and will increase GetHitEffect in an unpredictable speed because OnGUI is called 2 to 4 times between frames. Maybe moving this instruction in Update could at least make it work predictably.