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 galaboy · Oct 09, 2013 at 10:58 AM · scriptingbasicsjumping

java script to c# conversion

iam doing a small demo work using a tutorial. the tutorial is in javascript and iam doing it in c#. the script is about jumping. player has to jump when "w" is pressed, but the player is not staying on the ground surface it is going through the ground. i dont know where am making mistake. i player has rigid body and sphere collider and the ground surface has box collider. the code is below. need some help.

 using UnityEngine;
 using System.Collections;
 
 public class player_script : MonoBehaviour {
     
     public float speed = 10f;
     public float rotationSpeed = 30f;
     public GameObject projectile_Prefab;
     int score = 0;
     int lives = 0;
     public float time = 300f;
     
     public float jumpVelocity = 20f;
     public float maxSlope = 60f;
     public bool grounded = false;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         time -= Time.deltaTime;
         
         float amtToMove = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         transform.Translate(Vector3.right * amtToMove);
         transform.Rotate(amtToMove * rotationSpeed ,0, 0);
         
         if(Input.GetKeyDown("w") && grounded)
         {
             rigidbody.AddForce(new Vector3(0,jumpVelocity,0), ForceMode.Force);
         }
         
         if(Input.GetKeyDown("space"))
         {
             Vector3 position = new Vector3(transform.position.x + (transform.localScale.x / 2 + 0.5f),transform.position.y);
             Instantiate(projectile_Prefab,position, Quaternion.identity);
         }
     }
     
     void OnCollisionStay(Collision collision)
     {
         print("iam colliding");
         foreach(ContactPoint contact in collision.contacts)
         {
             if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
                 grounded = true;
         }
     }
     
     void OnCollisionExit()
     {
         grounded = false;
     }
     
     void OnCollisionEnter(Collision other) 
     {
         if(other.gameObject.name == "coins")
         {
             score += 10;
             Destroy(other.gameObject);
         }
     }
     
     void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 100, 20), "SCORE : " + score);
         GUI.Label(new Rect(200,10, 100, 20), "LIFE : " + lives);
         GUI.Label(new Rect(300,10, 100, 20), "TIME : " + time);
            
     }
     
 }
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
0
Best Answer

Answer by galaboy · Oct 10, 2013 at 07:33 AM

this is the code.

 using UnityEngine;
 using System.Collections;
 
 public class new_player_script : MonoBehaviour {
     
     public float speed = 10f;
     public float rotationSpeed = 30f;
     
     int score = 0;
     int lives = 0;
     public float time = 300f;
     
     public float jumpVelocity = 20f;
     public float maxSlope = 60f;
     public bool grounded = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         
         time -= Time.deltaTime;
         
         float amtToMove = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
         transform.Translate(Vector3.right * amtToMove);
         transform.Rotate(amtToMove * rotationSpeed ,0, 0);
         
         if(Input.GetKeyDown("w") && grounded)
         {
             rigidbody.AddForce(new Vector3(0,jumpVelocity,0), ForceMode.Force);
         }
     
     }
     
     void OnCollisionStay(Collision collision)
     {
         foreach(ContactPoint contact in collision.contacts)
         {
             if(Vector3.Angle(contact.normal, Vector3.up) < maxSlope)
                 grounded = true;
         }
         
     }
     
     void OnCollisionEnter(Collision other) 
     {        
         if(other.gameObject.name == "coins")
         {
             score += 10;
             Destroy(other.gameObject);
         }
     }
     
     void OnCollisionExit(){        
         grounded = false;
     }
 }
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 verenion · Oct 09, 2013 at 12:28 PM

Assuming you converted the code properly, then is the rigidbody set to Kinematic? if so, disable it. Can you link the JS code?

Comment
Add comment · Show 5 · 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 galaboy · Oct 09, 2013 at 12:38 PM 0
Share

previously i used only if(jump) and it worked. is kinematics are all disabled. this code is a from a video tutorial. i recreated it while watching it. i dont know whether there is a link to the code, but i'll try to find the link. thank for the suggestion.

avatar image verenion · Oct 09, 2013 at 12:40 PM 0
Share

print("iam colliding"); Does this output to the console at all?

avatar image galaboy · Oct 09, 2013 at 12:44 PM 0
Share

no it's not.

avatar image verenion · Oct 09, 2013 at 12:47 PM 1
Share

Okay. So, in that case grounded will ALWAYS be false because OnCollisionStay() is not being called. That might be because your colliders / rigidbodies are not setup correctly.

HOWEVER, I have just found this, and it looks like it's the same code. Try restarting Unity.

http://answers.unity3d.com/questions/142618/oncollisionstay-is-not-returning-anything.html

avatar image galaboy · Oct 10, 2013 at 07:32 AM 0
Share

thanks for the link. it works now by restarting unity and recreated the player with new player.

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

16 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

Related Questions

Adding Jumping and Falling Animations 0 Answers

!!!URGENT need help with gravity UGENT!!! 1 Answer

Make my FlashLight start off instead on. 1 Answer

JS and C# how to open in different programs. 1 Answer

Overwrite the value in my List but why? 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