Keep an object perpendicular to the surface it collides with and parallel to the floor
I am creating a small game where the player can shoot platforms that stick to the wall.
When a projectile hits a wall they are mostly perpendicular to the surface and parallel to the ground but not 100%, this can cause the player to slide off even with a high friction physics material. The prefab that is shot is an empty game object with a rigidbody and a box collider, The rotations are all frozen/constrained, there is a cube mesh but it does not have any physics components. The aim is to have the object always be at 90° to whatever surface it has impacted, the object scales along the X and Z-axis when it collides so that it is flat and acts as a platform.
I am fairly new to coding as such I do not know the necessary methods to calculate the two objects normals and make the platforms perpendicular.
Below is the collision detection code as well as my attempt at a very rough rotation fix that only works for certain angles since it will rotate around the Y and causes issues.
if(col.gameObject.tag == "Level")
{
rb.constraints = RigidbodyConstraints.FreezeAll;
cube.GetComponent<BoxCollider>().enabled = false;
scale = true;
StartCoroutine(ScaleOverTime(1));
StopCoroutine(ScaleOverTime(1));
StartCoroutine(DeScaleOverTime(1));
transform.localRotation = Quaternion.Euler
( transform.localRotation.x, 90f,transform.localRotation.z );
}'