- Home /
Translate this statement from .js to C#
I am doing a tutorial on CG cookie. the script is all in Javascript and I thought It would be a good exercise to try to write the same code in C#, I got stuck on implementing this function though. In Javascript it is:
`function CalculateAimPosition(targetPos : Vector3)
{
var aimPoint = Vector3(targetPos.x + aimError, targetPos.y+aimError, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}`
I am trying to write this in C# since my C# skills need work. I tried:
void CalculateAimPosition (Vector3 targetPos)
{
aimPoint = Vector3(targetPos.x + aimError, targetPos.y+aimError, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
I get an error that says 'void' cannot be used in this context. I know I am doing something wrong but I can't quite figure out what.
Answer by robertbu · Mar 10, 2014 at 03:10 AM
The error listed is likely to be a line or two before this function. As for this function, try this:
void CalculateAimPosition(Vector3 targetPos)
{
Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y+aimError, targetPos.z+aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
That worked, why did you use 'new Vector3' ins$$anonymous$$d of just vector3 in aimPoint = new Vector3 ?
The 'new' is required in C#. Take it out and see the compiler complain.
Answer by SirCrazyNugget · Mar 10, 2014 at 03:18 AM
//declared elsewhere as specified types (or int for aimError though doubtful)
float aimError;
Quaternion desiredRotation;
//should hopefully be something like this:
void CalculateAimPosition(Vector3 targetPos){
Vector3 aimPoint = Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
desiredRotation = Quaternion.LookRotation(aimPoint);
}
Just make sure something isn't trying to return a value from CalculateAimPosition and you're not already embedded in another function.
Your answer
Follow this Question
Related Questions
javascript equivalent of Action? 0 Answers
Multiple Cars not working 1 Answer
Lower player's health from separate script. 2 Answers