• 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 /
  • Help Room /
avatar image
0
Question by divyansh87 · Aug 26, 2015 at 11:51 AM · c#javascript

java script to c#

hi , i never learned/used java , i have a script ( i found it on internet ) is in the java script form ! but i need that in c# to add some more scripting to it ! the java script is working perfectly fine ! can anyone please tell me its C# equivalent ??

here is the script :

///////////////////////////// #pragma strict

var distToGround: float; var jumpspeed: float;

function Start(){ // get the distance to ground distToGround = GetComponent.().bounds.extents.y; }

function IsGrounded(): boolean { return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1); }

function Update () { if (Input.GetKeyDown(KeyCode.Space) && IsGrounded()){ GetComponent.().velocity.y = jumpspeed; } }

/////////////////////////////

Thanks : )

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

3 Replies

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

Answer by pako · Aug 26, 2015 at 12:15 PM

 float distToGround;
 float jumpspeed;
 Rigidbody rb;
 
 void Start(){
 
     // get the distance to ground
         distToGround = GetComponent<Collider>().bounds.extents.y;
         
         //cache Rigidbody component
         rb = GetComponent<Rigidbody>();
         
 }
 
 bool IsGrounded(){
 
     return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
     
 }
 
 void Update () {
 
     if (Input.GetKeyDown(KeyCode.Space) && IsGrounded())
     { 
         rb.velocity = new Vector3(0f, jumpspeed, 0f);
     }
     
 }

Comment
Add comment · Show 2 · 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 divyansh87 · Aug 26, 2015 at 12:34 PM 0
Share

@pako i am getting errors in this script ! how can i fix them ?

please have a look at them ( see the image ! )

alt text

capture.jpg (72.7 kB)
avatar image pako · Aug 26, 2015 at 02:14 PM 1
Share

Sorry about this, I did it in a hurry and missed a couple of details. I edited my code a little bit:

  1. Add an 'f' after 0.1, as in: distToGround + 0.1f -- in C# you have to add the 'f' to signify that the number is a float. This should take care of the first 2 errors.

  2. Assign a new vector to Rigidbody.velocity, rather than modifying only the 'y'. This should take care of the last error.

Let me know if this works.

avatar image
1

Answer by divyansh87 · Aug 27, 2015 at 11:55 AM

@pako thanks ,now this worked for me !! : ) you are great bro !

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 ventiv · Sep 27, 2015 at 07:51 PM

Here is a good place to go to so you don't have to constantly ask how to convert most unity 5 scripts will be easy to convert with this anything 4 and lower and you'll have problems

http://www.m2h.nl/files/js_to_c.php

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

28 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How add(create) a new gameobject on scene on mouse button click. 2 Answers

Need a Script for Changing Scenes after pressing E to open locked door once a key has been found. 0 Answers

UnityScript to C# Conversion 1 Answer

Need help in translating javascript code into c#. 1 Answer

JavaScript to C# 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