Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tintoy · Mar 15, 2011 at 03:25 AM · rigidbodycs1612

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);
}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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));
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Mar 15, 2011 at 03:47 AM 0
Share

I think it should be =, not += since his/her original script works by assigning y.

avatar image Mike 3 · Mar 15, 2011 at 03:56 AM 0
Share

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

avatar image Jessy · Mar 15, 2011 at 12:43 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges