• 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 bracciata · Aug 03, 2015 at 01:46 AM · java to c#

Simple transform position through velocity from JavaScript to C#

The code is javascript but I want to translate it too C# because I want to be able to refrence a different C# script. Thanks so much for the help.

GetComponent.().velocity.x = speed;

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

1 Reply

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

Answer by PatriceVignola · Aug 03, 2015 at 03:58 AM

To get a component in unity, you need to do GetComponent<ComponentType>(). For example, GetComponent<Rigidbody2D>() will give you the Rigidbody2D component of your GameObject, if it's attached.

This is because, contrary to Javascript, C# is a strongly typed language and GetComponent() is a generic method. This means you can do

 Rigidbody2D rb = GetComponent<Rigidbody2D>();

and not have to worry about runtime exceptions, which is a strong feature of object oriented languages.

Also, you can't generally directly assign the x, y or z values of the Unity Engine's components vectors in C# because they are implemented as properties instead of variables (which are getters and setters that you can call like variables), so you have to assign the whole vector to it. Here is what you could do:

 Rigidbody2D rb = GetComponent<Rigidbody2D>();
 rb.velocity = new Vector2(speed, rb.velocity.y);

That way, you change the x component of the velocity, and set the y component exactly as it was before. If you are working in 3D instead, you only need to use a Rigidbody instead of a Rigidbody2D component:

 Rigidbody rb = GetComponent<Rigidbody>();
 rb.velocity = new Vector3(speed, rb.velocity.y, rb.velocity.z);

If you are new to C# or object oriented languages in general, it might seem like a lot of new stuff, but it's worth it since those languages are so widely used. Here are a few links if you want to learn more:

https://msdn.microsoft.com/en-us/library/twcad0zb.aspx

https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

javascript to C# static function clamp to void 1 Answer

AndroidJavaObject that ships with c# API 0 Answers

Component error in C# [Solved] 1 Answer

Converting script to C# 1 Answer

Unity Android: Change ForwardNativeEventsToDalvik From Code 0 Answers


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