- Home /
Need help converting js to C#
I am new to Unity and i am trying to convert the CharacterControl script from the book "Unity 3 Game Development Hotshot". I have most of it but am having trouble with two things
JS - var v3_movement : Vector3 = (v3_moveDirection * f_moveSpeed) + Vector3 (0, f_verticalSpeed, 0);
C# - Vector3 v3_movement = (v3_moveDirection * f_moveSpeed) + Vector3 (0, f_verticalSpeed, 0);
The error i get is Expression denotes a `type', where a `variable', `value' or `method group' was expected
jS - public function IsGrounded () : boolean { return (c_collisionFlags & CollisionFlags.CollidedBelow); }
C# public bool IsGrounded() { return (c_collisionFlags & CollisionFlags.CollidedBelow); } the error here is Cannot implicitly convert type `UnityEngine.CollisionFlags' to `bool'
Any suggestions greatly appreciated
Cheers
Thankyou both.
the Vector3 line is now good.
I know that the CollisionFlags doesn't look like it should work but it does in the JavaScript code. Even when i add the !=0 i still get the same error.
This is where it also appears in the code - I will show the JS version that works
declared at top of class
private var c_collisionFlags : CollisionFlags;
in the Awake() function
c_collisionFlags = CollisionFlags.CollidedBelow;
in the Update() function
c_collisionFlags = controller.$$anonymous$$ove(v3_movement);
then in the IsGrounded function (bool) as in first post
return (c_collisionFlags & CollisionFlags.CollidedBelow);
Cheers
@Overflo: Don't post annotations as answers! Answers should answer the question. This is not a forum, this is a Q&A site.
I've converted your answer into a comment.
Answer by Khada · Aug 17, 2012 at 03:54 PM
You're missing the 'new' keyword:
Vector3 v3_movement = (v3_moveDirection * f_moveSpeed) + new Vector3 (0, f_verticalSpeed, 0);
The second one is more tricky. The & in c# is a bitwise AND. I'm not sure if it's the same in java (I suspect it is). Clearly 'CollisionFlags.CollidedBelow' isn't a boolian object and I can't see what type it is so it's hard to help here.
Answer by ScroodgeM · Aug 17, 2012 at 03:36 PM
new Vector3 (0, f_verticalSpeed, 0)
instead of
Vector3 (0, f_verticalSpeed, 0)
return (c_collisionFlags & CollisionFlags.CollidedBelow != 0)
instead of
return (c_collisionFlags & CollisionFlags.CollidedBelow)
Answer by Overflo · Aug 18, 2012 at 02:32 AM
Ahh I think I have it.
return (c_collisionFlags & CollisionFlags.CollidedBelow) != 0
instead of
return (c_collisionFlags & CollisionFlags.CollidedBelow != 0)
Thanks for your help
Answer by Spεci · Feb 08, 2013 at 02:36 PM
This one works with C#.
public bool IsGrounded() {
return (controller.collisionFlags == CollisionFlags.Below);
}
Although it seems that there is something wrong with the using of "&". Even the Unity documentation
suggests the using of "&" with C# but it gives the error that you mentioned.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
private Vector3 function? 2 Answers
[Dublicated, delete] Need help converting js to C# 1 Answer
Multiple Cars not working 1 Answer
Vector3 is always subject to serious stutter in Translation 0 Answers