Jump to content

Hi there, 

I am creating a flight simulation that focuses on the realistic delay due to varied factors. I'm not an experienced programmer or with unity.  I have a simple UI element within my game that displays the plane's (UAV) altitude, speed, distance from a base point and the latency. My issue is that the script used for this was in JavaScript and im no expert, so I need help with converting this to c# to get the figures as they update within the game, displayed on screen! Here is my code: 

var plane : Transform;
var b : Transform;

public var item2 : GUIContent = GUIContent("Altitude");
public var item3 : GUIContent = GUIContent("Speed");

private var guiSkin : GUISkin; 
var Alt:int;
var Spd:int;
var Bloc:int;
var latency: float;
var Distance:int;
var obritalSatellite:int;
//var PosZ:int;

function OnGUI(){ 
	GUI.skin = guiSkin;
	GUI.Box(Rect(0, Screen.height-120, 200, 120),"Plane");
	GUI.Label(Rect(20, Screen.height-80, 100, 50), "Altitude");
	GUI.Label(Rect(15, Screen.height-60, 100, 50), "Speed");
	GUI.Label(Rect(80, Screen.height-80, 100, 50), Alt.ToString());
	GUI.Label(Rect(80, Screen.height-60, 100, 50), Spd + " m/s".ToString());
	GUI.Label(Rect(10, Screen.height-40, 100, 50), "Base");
	GUI.Label(Rect(80, Screen.height-40, 100, 50), Distance + " m".ToString());
	GUI.Label(Rect(10, Screen.height-20, 100, 50), "latency");
	GUI.Label(Rect(80, Screen.height-20, 100, 50), latency + " ms".ToString());
}

function Update(){
Alt=plane.transform.position.y;
Spd=plane.GetComponent.<Rigidbody>().velocity.magnitude;
Bloc=b.transform.position.z;
//distance measurement from base to plane
var distance = Vector3.Distance(plane.transform.position, b.transform.position);

obritalSatellite = 70000;

Distance = distance;

//delay calculation
latency1 = (obritalSatellite + Distance) *0.000003 + 0.120;
latency = latency1;
}

My main issue is converting the function Update to c#.

 

Any help would be ideal, Thank You.

 

Regards, 

Andre

Link to comment
https://linustechtips.com/topic/750789-unity-ui-javascript-to-c-help/
Share on other sites

Link to post
Share on other sites

It's JavaScript, not Java.

Anyways, instead of function you use void and the way you declare variables is also a little different. I would suggest you check the Unity docs to see the differences.

 

If you are having any troubles, be sure to quote me with what parts are not working.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

11 minutes ago, Minibois said:

It's JavaScript, not Java.

Anyways, instead of function you use void and the way you declare variables is also a little different. I would suggest you check the Unity docs to see the differences.

 

If you are having any troubles, be sure to quote me with what parts are not working.

*JavaScript do forgive me. Thank you for your response. I've checked out the differences and some i am quite familiar with already, ive already started to replicate this to C#. My main problem id say is converting these few lines to C#: 

Alt=plane.transform.position.y;
Spd=plane.GetComponent.<Rigidbody>().velocity.magnitude;
Bloc=b.transform.position.z;

By chance is there anywhere specific that you could point me to in the Unity documents that could help? Thanks. 

 

p.s. I have checked 'transform.position' in the documents by the way, I understand that it creates

transform.position = new Vector3(0, 0, 0);

but I'm trying to set it to the variables (Alt, Spd etc...) rather than give it new xyz figures.

Link to post
Share on other sites

1 hour ago, Ledgend132 said:

*JavaScript do forgive me. Thank you for your response. I've checked out the differences and some i am quite familiar with already, ive already started to replicate this to C#. My main problem id say is converting these few lines to C#: 


Alt=plane.transform.position.y;
Spd=plane.GetComponent.<Rigidbody>().velocity.magnitude;
Bloc=b.transform.position.z;

By chance is there anywhere specific that you could point me to in the Unity documents that could help? Thanks. 

 

p.s. I have checked 'transform.position' in the documents by the way, I understand that it creates


transform.position = new Vector3(0, 0, 0);

but I'm trying to set it to the variables (Alt, Spd etc...) rather than give it new xyz figures.

The first and third line of the update should still work (provided plane and b are correctly declared), the second line (without me checking the Unity Documents) should also work as intended.

One way you could use the transform.position Vector3 is by adding whatever speed you need to the correct values (not sure how you want to do the controlling, but for example the throttle could increase the horizontal, usually z, speed..)

 

Also, it is recommend with controls and rigidbody movements, you used FixedUpdate, instead of Update. Update runs every frame of the game, while FixedUpdates runs an x amount of times per second, which is what you want for movement (otherwise more fps = faster gameplay)

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×