- Home /
How can i converting javascript to c#(error code CS1612)
Hi, I have to rewrite somecode. How can i covert javascript to c#. (rigidbodyObject is always moving.)
javascript here, (javascript is correct!)
function Update()
{
rigidbody.velocity.y=Mathf.Min(rigidbody.velocity.y,0.0);
}
c# code here, (but it is not correctly working.)
void Update()
{
rigidbody.velocity+=new Vector3(0F,(rigidbody.velocity.y,0F),0F);
}
Answer by Mike 3 · Mar 15, 2011 at 03:45 AM
Besides the compile issue, you're also changing the semantics of the statement
I'd change it to:
Vector3 vel = rigidbody.velocity;
vel.y = Mathf.Min(vel.y, 0);
rigidbody.velocity = vel;
That is actually how js does it in the background at any rate
Answer by Jessy · Mar 15, 2011 at 03:30 AM
rigidbody.velocity.y,0F doesn't mean anything. You forgot Mathf.Min.
rigidbody.velocity += new Vector3(0, Mathf.Min(rigidbody.velocity.y, 0));
I think it should be =, not += since his/her original script works by assigning y.
but then it'd set x and z, and you'd have to do = new Vector3(rigidbody.velocity.x, $$anonymous$$athf.$$anonymous$$in(rigidbody.velocity.y, 0), rigidbody.velocity.z) and at that point you're doing 4 rigidbody component lookups, 3 reads of velocity and 1 sec. ouch o.o
I don't think about that stuff so much because I always override all of the default components. [SerializeField] new Rigidbody rigidbody; for example, and always set that in the Editor. But you're right about the semantics issue. I didn't bother to look at the assignment.
Your answer
Follow this Question
Related Questions
Rigid Body Velocity in C# 4 Answers
AddTorque not increasing relativeVelocity magnitude of a collision 1 Answer
How do I know all objects have been destroyed? 1 Answer
A position locked hinge joint with drag and no gravity? 1 Answer
"Gluing" pieces together for a breakable object with springs 1 Answer