JavaScript (Unity spil) til C#
Jeg har nogle JavaScripts i et Unity (Game Enginen Unity) spil, jeg skal have lavet om til c#, men da jeg ikke har den store indsigt i JS finder jeg det lidt svært.Så er der nogle der kan hjælpe?
ConstructBoundary:
var mapAsset : TextAsset;
var blockPrefab : Transform;
var pelletPrefab : Transform;
var superPrefab : Transform;
function Awake () {
var map = mapAsset.text.Split ("\n"[0]);
var v = new Vector3 ();
v.y = 1.0;
var j_off = map.length / 2.0;
for (var j = 0; j < map.length; j ++) {
v.z = (map.length - j - j_off - 1) * 2;
var i_off = map[j].length / 2.0;
for (var i = 0; i < map[j].length; i ++) {
v.x = (i - i_off) * 2 + 1;
if (map[j][i] == "X") {
var inst = Instantiate (blockPrefab, v, Quaternion.identity);
inst.transform.parent = transform;
} else if (map[j][i] == ".") {
Instantiate (pelletPrefab, v, Quaternion.identity);
} else if (map[j][i] == "O") {
Instantiate (superPrefab, v, Quaternion.identity);
}
}
}
}
EatPellet:
var scoreDisplay : GUIText;
var smallPelletScore = 10;
var superPelletScore = 100;
private var score = 0;
function Update () {
scoreDisplay.text = "Score: " + score;
}
function OnTriggerEnter (other : Collider) {
if (other.name == "BasicPellet(Clone)") {
score += smallPelletScore;
} else if (other.name == "SuperPellet(Clone)") {
score += superPelletScore;
}
Destroy (other.gameObject);
}
GhostController:
var howLong = 1.0;
var howFast = 8.0;
private var nextUpdate = 0.0;
private var direction : Vector3;
function Update () {
if (Time.time > nextUpdate) {
nextUpdate = Time.time + (Random.value * howLong);
direction = Random.onUnitSphere;
direction.y = 0;
direction.Normalize ();
direction *= howFast;
direction.y = 1.5 - transform.position.y;
}
var controller = GetComponent(CharacterController);
controller.Move(direction * Time.deltaTime);
}
DragRigidbody:
var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;
private var springJoint : SpringJoint;
function Update ()
{
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
var mainCamera = FindCamera();
// We need to actually hit an object
var hit : RaycastHit;
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if (!springJoint)
{
var go = new GameObject("Rigidbody dragger");
body = go.AddComponent ("Rigidbody");
springJoint = go.AddComponent ("SpringJoint");
body.isKinematic = true;
}
springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
{
var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine ("DragObject", hit.distance);
}
function DragObject (distance : float)
{
var oldDrag = springJoint.connectedBody.drag;
var oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
springJoint.connectedBody.angularDrag = angularDrag;
var mainCamera = FindCamera();
while (Input.GetMouseButton (0))
{
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance);
yield;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag = oldDrag;
springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
}
function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
FPSWalker:
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
TimedObjectDestructor:
var timeOut = 1.0;
var detachChildren = false;
function Awake ()
{
Invoke ("DestroyNow", timeOut);
}
function DestroyNow ()
{
if (detachChildren) {
transform.DetachChildren ();
}
DestroyObject (gameObject);
}