Collision detection glitch
Below is my script, I am trying to stay away from unity's built in physics engine. For some reason, my player keeps falling through the floor when I start to move the play via the input keys (w,a,s,d, arrow keys) like in the pictures below.
// Declaration of Variables
void FixedUpdate(){
MovementManager();
}
//Function to set gravityEnabled bool value
bool GravityEnabled(){
RaycastHit hit;
Vector3 playerDown = new Vector3 (transform.position.x, -1f, transform.position.z);
if (Physics.Raycast(transform.position, playerDown, out hit)){
gravityEnabled=false;
}
else{
gravityEnabled=true;
}
return gravityEnabled;
}
void applyGravity(){
//simply apply a downward force
transform.position += (Vector3.down * Gravity * Time.deltaTime);
}
void MovementManager(){
float X = Input.GetAxis("Horizontal") * horzontal_Movement * Time.deltaTime;
float Y = Input.GetAxis("Vertical") * vertical_Movement * Time.deltaTime;
bool applGForce = GravityEnabled();
if (applGForce==true){
applyGravity();
transform.position += new Vector3 (X,0,Y);
}
if (applGForce==false)
transform.position += new Vector3 (X,0,Y);
}
I know this is a lot, but I just want an insight as to possibly why this is happening?
Thank you in advance.
Answer by HuracanBatman55 · Jan 25, 2017 at 11:14 PM
This has happened to me before and i always solve it by turning ridgidbody off. If it's vital to your game that rigidbody is on then i dont know.
Thanks for your reply.
Odd you would say that, I had no rigidbody attached to the player object at the time. As a temp fixed, maybe end up permanent, I just added the rigidbody for the collision detection. which seemed to solve the problem.
Your answer
Follow this Question
Related Questions
Player going through walls 1 Answer
When friction on physic material 2d is at 0 my player slide 0 Answers
Bounce, gravity and velocity 1 Answer
Rotating an object with VR controller 1 Answer
Smooth Walking Inside a Sphere 0 Answers