#How to rotate and object's Z angle with a 2D raycast hit?

1 messages · Page 1 of 1 (latest)

hoary drum
#

Hello I am currently trying to create rail grinding in my project. I created a test script that will rotate along the surface of the rail, but when testing it shows the Z rotation is changing but not the object itself?

Here is the code below:

    {
        Debug.DrawRay(startPoint.position, Vector3.down * lineDistance, Color.white);
        RaycastHit2D hit = Physics2D.Raycast(startPoint.position, Vector3.down * lineDistance, lineLayerMask);

        if ((hit.collider == null))
        {
            return;
        }
        else
        {
            Debug.Log($"{hit.collider.gameObject.transform.rotation.z}");

            transform.rotation = Quaternion.Euler(0f,0f ,hit.collider.gameObject.transform.rotation.z);
        }

    } ```
round hatch
#

if you want that object to rotate so it's down direction is towards the hit surface then just assign its transform.up to the hit.normal

hoary drum