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 chrisall76 · Aug 06, 2014 at 04:10 AM · rigidbodyfloatrigidbody physicsfloating

Rigidbody Floating Problem?

Hi all, I'm making a super smash bros clone(won't have the same characters), and it's going fine. I had to edit my movement script a bit recently since it had alot of unneeded things, but now when I jump and move left or right the rigidbody floats. Here's a webplayer to show how. Just go into training, jump (press P) and move left or right (A or D). or just see how slow it falls back after jumping in general. It didn't do this with the last script, but I can't use the last one since I want jumping to be delayed. https://dl.dropboxusercontent.com/u/85846965/SUB/SUB.html

I want jumping to have a small delay between when you press jump and it actually does it (already implemented), so UP+P attacks can work fine. Any help?

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     
     //Movement
     private Rigidbody _Rig; //Reference to the current rigidbody
     private bool CanJump = true; //If you can jump
     private float Speed = 5; //The speed we are currently going
     private float JumpSpeed = 10; //The speed of the jump we currently have
     [HideInInspector]
     public bool Moving = false;
     //[HideInInspector]
     public bool Grounded = true;
     //[HideInInspector]
     public int CJump = 0;
     [HideInInspector]
     public bool Jump = true;
     public bool Dashing = false; //Bool for if we're dashing
     public float Jumps = 1; //How many jumps you have
     public float WSpeed = 5; //Walking speed
     public float RSpeed = 8; //Running speed
     public float JSpeed = 10; //Jump velocity
     public float SJSpeed = 15; //Velocity of all jumps after the first
     public float RayDist = 5; //Distance of ray to check Grounded
     
     //General
     public int Player = 1; //The player's number, used for controller
     public float DecelSpeed = 0.9f; //Deceleration speed
     [HideInInspector]
     public bool Attk = false; //Checks if your attacking or not
 
     //Multi-Jump
     private bool CJumped = false; //If we have checked the last jump on the ground for jump delay
     public float JumpDelay = 0.75f; //Delay between touching the ground and being able to jump again
     public float JumpBDelay = 0.05f; //Delay before the jump to allow Up smashes
 
     void Update () {
         Jump = !Grounded; //Jump will never be true when your grounded
     }
     
     public void InputMan(float Hoz, float Vert, bool Jump){
         Grounded = Physics.Raycast (transform.position+new Vector3(0, 0.5f, 0), -transform.up, RayDist);
         
         //Limit Speed
         if(rigidbody.velocity.magnitude > WSpeed){
             rigidbody.velocity = rigidbody.velocity.normalized * WSpeed;
         }
         //Check Speed
         if (!Dashing) {
             Speed = WSpeed;
         } else {
             Speed = RSpeed;
         }
         
         //Movement
         if (Moving && Attk == false) {
             if(Vert < 0){
                 Vert = 0;
             }
             if(Grounded){
                 Vector3 temp = new Vector3(Hoz*WSpeed, 0, 0);
                 rigidbody.AddForce (temp, ForceMode.Impulse);
             }else{
                 Vector3 temp = new Vector3(Hoz*WSpeed/1.5f, 0, 0);
                 rigidbody.AddForce (temp, ForceMode.Impulse);
             }
         }
 
         //Checking if your grounded and it hasn't checked your jumps
         if (Grounded && CJumped == false) {
             Jump = false;
             StartCoroutine("JumpD");
             CJumped = true;
         }
 
         //First Jump
         if (Grounded && Jump && CanJump && Attk == false && CJump < Jumps) {
             Invoke("BJumpDelay", JumpBDelay);
         }
         //All Jumps After
         if (CJump > 0 && Jump && CanJump && Attk == false && CJump < Jumps) {
             Debug.Log("Double" + CJump.ToString());
             Jump = true;
             if(CJump == 0){
                 JumpSpeed = JSpeed;
             }else{
                 JumpSpeed = SJSpeed;
             }
             rigidbody.AddForce (transform.up*JumpSpeed, ForceMode.VelocityChange);
             CJump += 1;
             CJumped = false;
         }
 
         Decelerate (Hoz);
         LookDir ();
     }
 
     void BJumpDelay(){
         //Delay before you jump
         if(Attk == false){
             Jump = true;
             if(CJump == 0){
                 JumpSpeed = JSpeed;
             }else{
                 JumpSpeed = SJSpeed;
             }
             rigidbody.AddForce (transform.up*JumpSpeed, ForceMode.VelocityChange);
             CJump = 1;
             CJumped = false;
         }
     }
 
     void LookDir(){
         //Look in direction of movement
         if (Moving) {
             Vector3 tempT = rigidbody.velocity;
             tempT.z = tempT.x;
             tempT.y = 0;
             tempT.x = 0;
             transform.rotation = Quaternion.LookRotation(tempT);
         }
     }
 
     void Decelerate(float Hoz){
         //Checks if moving, and also decelerates once you stop moving to prevent sliding
         if (Hoz > 0 && !Attk || Hoz < 0 && !Attk) {
             Moving = true;
         } else {
             Moving = false;
             if(Grounded){
                 rigidbody.AddForce(rigidbody.velocity.x*(-transform.right*DecelSpeed));
             }
         }
     }
     
     IEnumerator JumpD(){
         //Delay of jump after you hit the ground
         CanJump = false;
         yield return new WaitForSeconds(JumpDelay);
         CanJump = true;
         CJump = 0;
     }
 
     IEnumerator JumpAnimD(){
         //Animation data for jumping
         Jump = true;
         yield return new WaitForSeconds(0.75f);
         Jump = false;
     }
 }
Comment
Add comment · Show 1
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 iwaldrop · Aug 06, 2014 at 04:55 AM 0
Share

Have a look at the Input$$anonymous$$an method, and specifically the limit speed section in there. If you notice, while you're falling, if you also press left or right while you do, then you fall even more slowly. If you want to fall quickly but limit lateral movement, operate on only the x component of the velocity.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Noob_Vulcan · Aug 06, 2014 at 04:49 AM

  if(Grounded){
 Vector3 temp = new Vector3(Hoz*WSpeed, 0, 0);
 rigidbody.AddForce (temp, ForceMode.Impulse);
 }else{
 Vector3 temp = new Vector3(Hoz*WSpeed/1.5f, 0, 0);
 rigidbody.AddForce (temp, ForceMode.Impulse);
 }

The problem is in the else part .When the JigglyPuff is not grounded this

rigidbody.AddForce (temp, ForceMode.Impulse);

code runs continuously. This is some how related to you Input mechanism way. Just call this code once by ading a bool to it

i think this should solve it.

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 chrisall76 · Aug 06, 2014 at 05:41 AM 0
Share

doesn't seem to. Also, that code is how I actually move left/right atm, but I timed each time with and without that code and they where the same. Also, the sprite is of $$anonymous$$irby lol

avatar image Noob_Vulcan · Aug 06, 2014 at 08:04 AM 0
Share

@chrisall76 when you jump "$$anonymous$$IRBY" the way it comes down without pressing left/right key . It comes very slowly . Thats is also ur problem?

Or lets say u want "$$anonymous$$IRBY" to jump n fall as a normal jump we see in games?

avatar image
0

Answer by chrisall76 · Aug 11, 2014 at 03:14 PM

Would like to say I found the problem, and it was pretty simple.

         if(rigidbody.velocity.magnitude > WSpeed){
             rigidbody.velocity = rigidbody.velocity.normalized * WSpeed;
         }

This line also limits speed on the vertical axis, which is what causes the floating/hovering. This should be used instead.

         Vector3 velocity = rigidbody.velocity;
         velocity.x = Mathf.Clamp(velocity.x, -WSpeed, WSpeed);
         rigidbody.velocity = velocity;
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.

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

RigidbodyConstraints.FreezePositionY on button press 0 Answers

Keeping momentum with rigid bodies. 0 Answers

Kinematic rigidbody movement. 2 Answers

Rigidbody Glitch 1 Answer

How to Dash smoothly and Jump fall faster with a 3D rigidbody? 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