- Home /
Question by
Follett · Jun 21, 2011 at 12:53 PM ·
Help converting JavaScipt too c#
I am trying to change this code from JavaScript to C# and was wondering if anyone could help me out. Although I will edit the code it should find the position of the nearest object that has the tag and then will insert it into a variable.
function GetNearestTaggedObject() : Transform {
// and finally the actual process for finding the nearest object:
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
var nearestObj : Transform = null;
// loop through each tagged object, remembering nearest one found
for (var obj : GameObject in taggedGameObjects) {
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestObj = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
return nearestObj;
}
Thanks everyone.
Comment
Best Answer
Answer by ckfinite · Jun 21, 2011 at 01:01 PM
public Transform GetNearestTaggedObject() {
// and finally the actual process for finding the nearest object:
var nearestDistanceSqr = Mathf.Infinity;
var taggedGameObjects = GameObject.FindGameObjectsWithTag(searchTag);
Transform nearestObj = null;
// loop through each tagged object, remembering nearest one found
foreach (var obj in taggedGameObjects) {
var objectPos = obj.transform.position;
var distanceSqr = (objectPos - transform.position).sqrMagnitude;
if (distanceSqr < nearestDistanceSqr) {
nearestObj = obj.transform;
nearestDistanceSqr = distanceSqr;
}
}
return nearestObj;
}
Your answer
Follow this Question
Related Questions
how would i write this in C#? 1 Answer
split screen multiplayer car game? 2 Answers
Quaternion .js to c# 2 Answers
JS to C#, anyone? 1 Answer
Need help converting js to C 1 Answer