Rigidbody randomly fall through floor
Hi everyone I had a weird issue with my project. When I make my character jump, he randomly fall through the floor like this: http://plays.tv/video/588b59f23a5950461c/wtf-unity
I don't understand why it's happening.
Here is the script of the character controller that is affecting this rigibody:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
float move;
public float moveSpeed = 12f;
public bool isJumping = false;
public bool grounded = false;
public bool toped = false;
float jumpForce = 175f;
float jumpTime;
float jumpMaxTime = 0.3f;
public GameObject gameController; // On récupère le GameObject contenant le GameController
GameController gameControllerScript; // Variable pour stocker le script GameController
// Use this for initialization
void Start () {
gameControllerScript = gameController.GetComponent<GameController>(); //On récupère le script GameController
}
// Update is called once per frame
void Update () {
move = Input.GetAxis("Horizontal");
GetComponent<Rigidbody>().velocity = new Vector3 ( move*moveSpeed , GetComponent<Rigidbody>().velocity.y - gameControllerScript.gravity, GetComponent<Rigidbody>().velocity.z);
print (grounded);
if (grounded) {
if (Input.GetButtonDown ("Jump"))
{
jumpTime = 0f;
isJumping = true;
}
}
if (isJumping) {
jumpTime+= Time.deltaTime;
if (((toped) || jumpTime >= jumpMaxTime || Input.GetButtonUp("Jump")) & (jumpTime > 0.3f)) {
isJumping = false;
}
GetComponent<Rigidbody>().AddForce(Vector3.up * jumpForce);
}
}
}
And here is some screenshots from Unity:
What is happening and what can I do to fix this? If you have any things you can say about my code don't hesitate as well if you have any advice :)
Use FixedUpdate ins$$anonymous$$d of Update. You're messing with physics in the wrong loop.
Your answer
Follow this Question
Related Questions
Rigidbody going through wall during pinch-to-zoom 0 Answers
Rigidbody going through Colliders help! 1 Answer
How to Make A Character Stop At Wall? 0 Answers
Detecting collisions on an invisible grid 0 Answers
Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer