Checking whether player is standing on terrain or object,Checking if player is on terrain or object
I'm somewhat of a newbie to unity. And I'm currently trying to develop a system of changing the sound of the player's footsteps if they walk onto an object.
Currently, I have the following system set up in a larger audio manager class which is attached to the player. It checks to see if the player is colliding with the terrain or an object (which is then used elsewhere to determine footstep sounds):
void GroundOrObjCheck(Collision collision)
{
if (collision.gameObject.tag == "Terrain")
{
Debug.Log("Player colliding with terrain");
playerIsOnTerrain = true;
return;
}
else if (collision.gameObject.tag == "Object")
{
Debug.Log("Player colliding with object");
playerIsOnTerrain = false;
//Finds name of material of the current object player is colliding with
materialName = collision.gameObject.GetComponent<Renderer>().material.name;
return;
}
}
void OnCollisionEnter(Collision collision)
{
GroundOrObjCheck(collision);
}
My issue is, is that the game is detecting collisions very sporadically. It only detects the collision of the wooden test platform if I hit it from the side and doesn't detect colliding with the terrain at all. I've tried adding a rigidbody component to both the terrain and test platform (with IsKinematic = true) but with no luck.
Below is a pic of my setup for concept
,I'm somewhat of a newbie to unity. And I'm currently trying to develop a system of changing the sound of the player's footsteps if they walk onto an object.
Currently, I have the following system set up in a larger audio manager class which is attached to the player. It checks to see if the player is colliding with the terrain or an object (which is then used elsewhere to determine footstep sounds):
void GroundOrObjCheck(Collision collision)
{
if (collision.gameObject.tag == "Terrain")
{
Debug.Log("Player colliding with terrain");
playerIsOnTerrain = true;
return;
}
else if (collision.gameObject.tag == "Object")
{
Debug.Log("Player colliding with object");
playerIsOnTerrain = false;
//Finds name of material of the current object player is colliding with
materialName = collision.gameObject.GetComponent<Renderer>().material.name;
return;
}
}
void OnCollisionEnter(Collision collision)
{
GroundOrObjCheck(collision);
}
My issue is, is that the game is detecting collisions very sporadically. It only detects the collision of the wooden test platform if I hit it from the side and doesn't detect colliding with the ground at all. I've tried adding a rigidbody component to both the terrain and test platform (with IsKinematic = true) but with no luck.
Below is a pic of my setup for concept
Answer by CZNextik · 5 days ago
Did you try raycasting from the player feet? That should do the job better.