Issues rotating object to face normal
I'm trying to get an object to face a raycast hit normal while still staying upright. Here's a recording of what I have now: http://files.hahndynasty.net/0U3Y2a0A3G14
As intended, the flyswatter stays upright on the wall. I'm achieving this by manually setting the x rotation to 270 unless the absolute value of the normal y is 1. If the absolute value of the y normal is 1, the x rotation is set to 0. This works for most of the ceiling, but fails in some parts of it. I've checked and the normal y in these parts of the ceiling is still 1. Whenever the flyswatter moves onto the floor, the position and rotation resets back to default. I have no idea what's causing this! Any help would be appreciated. Here's my code:
transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
if(Mathf.Abs(hit.normal.y) != 1){
transform.eulerAngles = new Vector3(270, transform.eulerAngles.y, transform.eulerAngles.z);
}
else{
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, transform.eulerAngles.z);
}
transform.position = hit.point;
Debug.Log(hit.normal);
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612074349im_/https://answers.unity.com/themes/thub/images/avi.jpg)