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.