Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Miigman · Oct 21, 2017 at 10:07 PM · rigidbodyinputjumpinggameplay

Rigidbody Jumping issues

Hey guys!

I've been programming a game for quite some time now, have had it released a few times, including on the Wii U and I, as well as my players have been running into a random issue. In the game you control a speeding ship, you are able to control the throttle and movement while jumping onto platforms.

The problem is, at times my ship doesn't always jump. It seems to be when the ship is nearing the end of a platform, when you hit the jump button the ship doesn't jump, causing the player to die.

Here's my code, keep in mind please, I have taught myself coding and realize there is nothing fancy here, in fact it's quite messy code. But if you have any idea, or suggestions please do let me know!

I've been working on it for the past few days and as you can see I've added multiple redundancies to force grounding as I assumed it was an issue with the ship not detecting it was on the ground properly... anyways, any suggestions would be great!

 #pragma strict
 
 var jump : float = 55;
 var secondsLeft : float = 0.5;
 var Speed        : float = 0;//Don't touch this
 var ForwardSpeed : float = 0;//Don't touch this
 var MaxSpeed     : float;//This is the maximum speed that the object will achieve
 var Acceleration : float = 10;//How fast will object reach a maximum speed
 var Deceleration : float;//How fast will object reach a speed of 0
 var score : float;
 var isGrounded : boolean = true;
 var Explosion : GameObject;
 var Splash : GameObject;
 var Ship : Transform;
 var BumpSound : AudioClip;
 var SplashSound : AudioClip;
 var EngineSound :AudioClip;
 var CollectSound : AudioClip;
 var yourSpeed = 0;
 var yourMaxSpeed = MaxSpeed;
 //var rate : float;
 var customSkin : GUISkin;
 var Jumpomatic : String;
 var myPosition : Vector3;
 var LJoyPos : float;
 var RJoyPos : float;
 var gameObjects : GameObject[];
 static var collectedGas : boolean = false;
 var gravo : String;
 public var amount : float = 50f;
 var distToGround : float;
 
 function OnGUI ()
 {
     GUI.skin = customSkin;
     var kph = ForwardSpeed;
     GUI.Label(Rect (600, 20, 500, 25), kph.ToString("F0") + " MPH");
     //GUI.Label(Rect (600, 100, 500, 25), RJoyPos.ToString("F3"));
     //GUI.Label(Rect (600, 25, 200, 25), "Speedometer");
     GUI.Label(Rect (1050, 50, 200, 25), "Jump-o-matic"); 
     GUI.Label(Rect (1100, 20, 200, 25), Jumpomatic);
     GUI.Label(Rect (800, 50, 350, 25), "Gravo-Meter"); 
     GUI.Label(Rect (800, 20, 350, 25), gravo);
     //GUI.Label(Rect (350, 20, 500, 25), score.ToString("F0"));
 
     //GUI.HorizontalScrollbar(Rect (595,10,200,25), 0, yourSpeed,0, yourMaxSpeed);
 }
 
 function OnTriggerEnter (other : Collider)
 {
     //check if it's the player that is colliding with this reset object by name/tag or anything else you want
     if (other.name == "Change Song")
     {
         GetComponent.<AudioSource>().clip = SplashSound;
         GetComponent.<AudioSource>().Play();
     }
     if (other.tag == "Collectible") { 
     score += 1; 
     GetComponent.<AudioSource>().clip = CollectSound;
     GetComponent.<AudioSource>().Play();
     PlayerPrefs.SetInt("Score", score);
     Destroy(other.gameObject); 
 }
     
     
     
     }
     
 
 
 function Start () {
     score = (PlayerPrefs.GetInt("Score"));
     distToGround = GetComponent.<Collider>().bounds.extents.y;
     isGrounded = true;
     
 }
 
 function OnCollisionStay(collision : Collision)
 {
    if(collision.gameObject.layer == LayerMask.NameToLayer("Platform"))
     {
         isGrounded = true;
         //audio.PlayOneShot(BumpSound);
     } 
     if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
     {
         isGrounded = true;
         //audio.PlayOneShot(BumpSound);
     } 
 }
 
 function OnCollisionExit(collision : Collision)
 {
 
    if(collision.gameObject.layer == LayerMask.NameToLayer("Platform"))
     {
         isGrounded = false;
         //audio.PlayOneShot(BumpSound);
     } 
     if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
     {
         isGrounded = false;
         //audio.PlayOneShot(BumpSound);
     } 
 }
 
 function OnCollisionEnter(collision : Collision)
 {
     if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
     {
     var rigidBody = GetComponent.<Rigidbody>();
     rigidBody.isKinematic = true;
         isGrounded = true;
        //audio.PlayOneShot(BumpSound);
     } else if (!CheckGrounding) {
         // we must have hit something in mid-air!
         Debug.Log("Ooof!");
     }
    
 
     
      if(collision.gameObject.layer == LayerMask.NameToLayer("Platform"))
     {
     //var rigidBody = GetComponent.<Rigidbody>();
     rigidBody.isKinematic = true;
         isGrounded = true;
         //audio.PlayOneShot(BumpSound);
     } 
 
      if((collision.gameObject.layer == LayerMask.NameToLayer("Platform"))&&(CheckGrounding))
     {
         isGrounded = true;
         CheckGrounding();//audio.PlayOneShot(BumpSound);
     } 
     
 
     
     
     if(collision.gameObject.name == "Death")
     {
     score = 0; 
     PlayerPrefs.SetInt("Score", score);
     myPosition = transform.position;
     Instantiate(Explosion, new Vector3(myPosition.x,myPosition.y, myPosition.z), Quaternion.identity);
       Destroy (gameObject);
     }
     
      if(collision.gameObject.layer == LayerMask.NameToLayer("Secret"))
     {
     
     }
     
     if(collision.gameObject.layer == LayerMask.NameToLayer("Water"))
     {
     GetComponent.<AudioSource>().PlayOneShot(SplashSound);
     myPosition = transform.position;
     Instantiate(Splash, new Vector3(myPosition.x,myPosition.y, myPosition.z), Quaternion.identity);
       Destroy (gameObject);
     }
     
     if(collision.gameObject.layer == LayerMask.NameToLayer("Gas"))
     {
         collectedGas = true;
         
     }
     
     if(collision.gameObject.layer == LayerMask.NameToLayer("Bounce"))
     {
     isGrounded = false;
     GetComponent.<Rigidbody>().AddForce(Vector3.up * 3500);
     }
     if(collision.gameObject.layer == LayerMask.NameToLayer("Slow"))
     {
     isGrounded = true;
     if (ForwardSpeed > 0) {
     ForwardSpeed = ForwardSpeed - Acceleration * 500 * Time.deltaTime;
     }
     if (ForwardSpeed < 0) {
     ForwardSpeed = 0;
     }}
 }
      
 
 
 
 
 
 function Update () {
 distToGround = GetComponent.<Collider>().bounds.extents.y;
     CheckGrounding();
 //RJoyPos = Input.GetAxis ("Horizontal");
 //LJoyPos = Input.GetAxis ("RStick Up/Down");
 yourSpeed = ForwardSpeed;
 var fireRate : float = .5;
 var nextFire : float = 2;
 
 var h : float = Input.GetAxis("Horizontal") * amount * Time.deltaTime;
     var v : float = Input.GetAxis("Vertical") * amount * Time.deltaTime;
     
   
 
 
 if ((Input.GetAxis("Horizontal")<0)&&(Speed < MaxSpeed))
     Speed = Speed - Acceleration * Time.deltaTime;  
 else if ((Input.GetAxis("Horizontal")>0)&&(Speed > -MaxSpeed))
     Speed = Speed + Acceleration * Time.deltaTime;
 else
 {
     if(Speed > Deceleration * Time.deltaTime)
         Speed = Speed - Deceleration * Time.deltaTime;
     else if(Speed < -Deceleration * Time.deltaTime)
         Speed = Speed + Deceleration * Time.deltaTime;
     else
         Speed = 0;
 }
 
 
 
 
 transform.position.x = transform.position.x + Speed * Time.deltaTime;
 
 
 if ((Input.GetAxis("Vertical")<0)&&(ForwardSpeed > 0))
     {
     ForwardSpeed = ForwardSpeed - Acceleration * 4 * Time.deltaTime;  
     }
     else if ((Input.GetAxis("Vertical")<0)&&(ForwardSpeed <= 0)){
     ForwardSpeed = 0;
     }
     
 
 if (Input.GetAxis("Vertical")>0)
 if (ForwardSpeed <= 150) {
     ForwardSpeed = ForwardSpeed + Acceleration * Time.deltaTime;
     }
 else
 {
    if(ForwardSpeed > Deceleration * Time.deltaTime)
         ForwardSpeed = ForwardSpeed - Deceleration * Time.deltaTime;
    else if (ForwardSpeed < -Deceleration * Time.deltaTime)
        ForwardSpeed = ForwardSpeed + Deceleration * Time.deltaTime;
     else
         ForwardSpeed = 0;
 }
 
 
 if (Application.loadedLevel == 19) {
     ForwardSpeed = 150;
 }
 
 if (Application.loadedLevel == 20) {
     ForwardSpeed = 150;
 }
 
 transform.position.z = transform.position.z + ForwardSpeed/1.5 * Time.deltaTime /2;
 
 if (Input.GetAxis("Escape")) {
 
 Level_Select.levelToLoad = 3;
                Application.LoadLevel(23); 
               // Application.LoadLevel(3);
 }
 
 
 }
 
   function FixedUpdate()
   {
     Debug.Log(Input.inputString);
     
     
     
      var hit = new RaycastHit();
      var dist : float = .5;
      var dir : Vector3;
 
      dir = Vector3(0,-1,0);
 
      if (Physics.Raycast(transform.position,dir,hit,dist))
                     {
                         if (hit.collider.gameObject.tag == "Platform"){
                             isGrounded = true;
                         }
                         if (hit.collider.gameObject.tag == "Gas"){
                             isGrounded = true;
                         }
 
                         if (hit.collider.gameObject.layer == LayerMask.NameToLayer("Water")) 
                         {
                             GetComponent.<AudioSource>().PlayOneShot(SplashSound);
                             myPosition = transform.position;
                             Instantiate(Splash, new Vector3(myPosition.x,myPosition.y, myPosition.z), Quaternion.identity);
                             Destroy (gameObject);
 
                         }
                         
                     }
 
      if (Input.GetAxis("Jump"))
         {
         var rigidBody = GetComponent.<Rigidbody>();
             CheckGrounding();
                     if (Physics.Raycast(transform.position,dir,hit,dist))
                     {
                             if (rigidBody.velocity.y != 0)
                             {
                                 GetComponent.<Rigidbody>().velocity.y = 0;
                             }
                         if (hit.collider.gameObject.tag == "Platform"){
                             
                             GetComponent.<AudioSource>().PlayOneShot(BumpSound);
                             GetComponent.<Rigidbody>().AddForce(new Vector3(0, jump,0), ForceMode.Impulse);
                             isGrounded = false;
                             //Debug.Log("Player is on " + hit.collider.gameObject.name);
                         }
                         if (hit.collider.gameObject.tag == "Gas"){
                             
                             GetComponent.<AudioSource>().PlayOneShot(BumpSound);
                             GetComponent.<Rigidbody>().AddForce(new Vector3(0, jump,0), ForceMode.Impulse);
                             isGrounded = false;
                             //Debug.Log("Player is on " + hit.collider.gameObject.name);
                         }
                     }    
         }
             if (!CheckGrounding) 
             {
                 //isGrounded = false;
                 
             }
             else 
             {
             //    isGrounded = true;
             //    Jumpomatic = "Ready";
             }
 
             if (isGrounded == false)
             {
                 Jumpomatic = "Active";
             }
             else{
                 Jumpomatic = "Ready";
             }
 }
 
 function CheckGrounding() : boolean 
 {
     return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
     //Debug.Log(CheckGrounding());
 }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Unity skipping input 0 Answers

Why is my player getting stuck when jumping into awalls? 0 Answers

please help me with my jumping script 1 Answer

Input AnyKey only working for one of my players? 0 Answers

Why does the player hover over the ground instead of touching it? Why doesn't it always jump when I press space? 2 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