- Home /
Random velocity changes
I have a character that moves with rigidbody. When i jump, i looks find, but sometimes, my character boost up while in the air and get out of the map very fast. I checked, and when it happens, it says the velocity is at about 210 or around it. i just happen when i walk and i jump. It happen like 5% of the time. why is it happening and how can i fix it ?`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class playerManager : MonoBehaviour
{
float xMovement;
float zMovement;
float xPosition;
float yPosition;
float zPosition;
public float runningSpeed;
public Animator animator;
public Camera camera;
public float cameraHeight;
public float cameraAngle;
public bool isOnGround;
public float jumpForce;
Rigidbody playerRigidbody;
public float gravityMultiplier;
Vector3 vel;
public float airSpeed;
public float groundSpeed;
public bool canJump = true;
void Start()
{
playerRigidbody = GetComponent<Rigidbody>();
vel = playerRigidbody.velocity;
}
void Update()
{
xPosition = transform.position.x;
yPosition = transform.position.y;
zPosition = transform.position.z;
PlayerMovements();
CameraManager();
}
public void CameraManager()
{
camera.transform.position = new Vector3(xPosition+cameraAngle,cameraHeight,zPosition+cameraAngle);
}
public void PlayerMovements()
{
//Jump
if(Input.GetKeyDown(KeyCode.Space) & isOnGround == true & canJump == true)
{
runningSpeed = airSpeed;
canJump = false;
playerRigidbody.velocity = new Vector3(xMovement,0,zMovement);
playerRigidbody.angularVelocity = Vector3.zero;
playerRigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
playerRigidbody.AddForce(transform.up * jumpForce);
playerRigidbody.drag = 0;
}
//Move the Character
if(isOnGround == true)
{
xMovement = Input.GetAxis ("Horizontal");
zMovement = Input.GetAxis ("Vertical");
Vector3 distanceToMove = new Vector3(xMovement,0,zMovement);
playerRigidbody.AddForce(distanceToMove * runningSpeed);
}
//Make the player face the direction he's going at
transform.LookAt(new Vector3(xPosition+xMovement,yPosition,zPosition+zMovement));
//Trigger Idle or Running Animations
if(xMovement != 0 | zMovement != 0)
{
animator.SetBool("Run", true);
animator.SetBool("Idle", false);
}
if((xMovement < 0.05f & xMovement > -0.05f) & (zMovement < 0.05f & zMovement > -0.05f))
{
animator.SetBool("Run", false);
animator.SetBool("Idle", true);
}
}
public void OnCollisionEnter(Collision collidedOBJ)
{
if(collidedOBJ.gameObject.tag=="Finish")
{
Debug.Log(playerRigidbody.velocity);
Debug.Log(runningSpeed);
}
if(collidedOBJ.gameObject.name == "Ground")
{
playerRigidbody.drag = 49;
playerRigidbody.constraints &= RigidbodyConstraints.FreezePositionY|RigidbodyConstraints.FreezeRotationX|RigidbodyConstraints.FreezeRotationY|RigidbodyConstraints.FreezeRotationZ;
isOnGround = true;
canJump = true;
runningSpeed = groundSpeed;
}
}
public void OnCollisionExit(Collision exitCollidedOBJ)
{
if(exitCollidedOBJ.gameObject.name == "Ground")
{
isOnGround = false;
}
}
}
`
Honestly, that all looks really complicated to handle just a jump:
//Jump
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) & isOnGround == true & canJump == true)
{
runningSpeed = airSpeed;
canJump = false;
playerRigidbody.velocity = new Vector3(x$$anonymous$$ovement,0,z$$anonymous$$ovement);
playerRigidbody.angularVelocity = Vector3.zero;
playerRigidbody.constraints &= ~RigidbodyConstraints.FreezePositionY;
playerRigidbody.AddForce(transform.up * jumpForce);
playerRigidbody.drag = 0;
}
I would use a premade character controller till you have a good understanding of code; character controllers are basically the most proprietary part of handling movement and knowing what state the character is in and what state they can transition to, etc. Character controllers can get really complicated really quickly as you add more features that need to check on other character states.
I'm not giving an answer because there is a lot more going on with that script you provided that needs to be addressed than just a floaty jump; Unity has character controllers you can download for free regardless if you have a first, third, or sidescrolling game; take a look at those scripts and get ideas from them:
https://assetstore.unity.com/packages/essentials/asset-packs/standard-assets-32351
https://assetstore.unity.com/packages/essentials/tutorial-projects/2d-game-kit-107098
https://assetstore.unity.com/packages/templates/tutorials/3d-game-kit-115747
There is a reason why games like $$anonymous$$ario, Super $$anonymous$$eat Boy, and Celeste are more fun to play than a lot of other platformers, and it is because their proprietary character controller; I would use someone else's until you understand code a lot better.
The reason it is so complicated it because ive been trying to fix my problem for over 2h, so i got to add lot of useless stuff. And the reason why its so complicated is because i have 49 of drag but i have to remove it to jump and than change the speed (because it would be way to fast without the drag). To give you an idea, on the ground i need a 15000 speed and in the air i need a 10 speed. but if you have any idea on how to fix this well im listening to you. And by the way I just dont want to use anything that isnt made by me for this game, hope you can understand.
I can try to find your bug in a few $$anonymous$$utes but otherwise, I agree with I_Am_Error this is a bit confused. A couple of things I want to immediately mention: $$anonymous$$ethods are not things. They are actions.
... so thinking about them as in Player$$anonymous$$ovements or Camera$$anonymous$$anager you may think is trivial but its definitely contributing to your confusion.
Think about them ins$$anonymous$$d as
"$$anonymous$$ove" or "$$anonymous$$anageCamera"
i.e. doing something rather than being something.
It will also outline that your methods are perhaps far too broad. They should do one thing and and do it well.
So split your jump code out into its own method for example. Let me cook dinner then I can come and help with your bug.
Okay. Thanks for your time and your advices. I'll redo the code tonight to remove the useless stuff and ordering it.