- Home /
Can you help me convert this to C#?
var target : Transform; //the enemy's target var moveSpeed = 3; //move speed var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake() { myTransform = transform; //cache transform data for easy access/preformance }
function Start() { target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () { //rotate to look at the player myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
"That's not what Answers is really about. At least try to convert it yourself and then if you get stuck on a particular area I'm sure lots of the folks here can help." http://answers.unity3d.com/questions/17570/can-i-convert-c-sharp-to-javascript/17572#17572
No, we won't. We don't write your code for you. Learn it yourself.
Here's some links I found useful in converting between C# and JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
Answer by tertle · Jan 27, 2011 at 10:25 PM
Not really sure why you would be coding in c# if you don't know how to write it, but here it is converted anyway. Rename the class.
[System.Serializable] public class Move : MonoBehaviour { public Transform Target; public float MoveSpeed = 3f; public float RotationSpeed = 3f;
private Transform _myTransform;
void Awake()
{
_myTransform = transform;
}
void Start()
{
Target = GameObject.FindWithTag("Player").transform;
}
void Update()
{
_myTransform.rotation = Quaternion.Slerp(_myTransform.rotation, Quaternion.LookRotation(Target.position - _myTransform.position), RotationSpeed * Time.deltaTime);
_myTransform.position += _myTransform.forward*MoveSpeed*Time.deltaTime;
}
}
The reason why is because this person (as well as myself) are new to scripting and found the above script when looking for a basic enemy AI. Because Javascript will not open, the script cannot be executed. Thank you for translating it, greatly appreciated.
Your answer
Follow this Question
Related Questions
Change AI Follow Players 1 Answer
Object where the mouse is 4 Answers
How can I get this script to work? 1 Answer
Animation Stops Unexpected. (Script included) 0 Answers
Jump help! 1 Answer