- Home /
Problem with normal snapping and rotations.
I have a game where you can build mini-golf holes and place things on them, and I wanted to make it so that the objects you are placing match up with the normals of the hole because there might be an elevation change in the course. I have the hole "decorating" or object-placing script here, but I will show the main part of the code that is doing it below.
So to start, I set the position of the object to another object which moves to the mouse's position on the ground.
if (!isRotating)
{
//Movement
pendingObject.transform.position = buildingPosObj.transform.position;
(The isRotating bool is for another form of rotation, I have two. The first is just pressing R to rotate the object 45 degrees, the second is holding R to freely rotate it, and it shouldn't move when freely rotating it)
That part works fine. But the next part is what is causing the problem. If you would like to see the bug, click here. This is the part of the code that makes the rotation:
Vector3 up = hit.normal;
Vector3 forward = Vector3.Cross(Vector3.up, up);
Vector3 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, Quaternion.LookRotation(forward, up).eulerAngles.y + pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
pendingObject.transform.eulerAngles = finalRot;
So I know the problem is because I am adding the new rotation to the old one, but I don't really know any efficient and clean ways to fix this. I have tried one thing, but it still adds it to the rotation whenever the normal changes:
if(hit.normal != previousNormal)
{
previousNormal = hit.normal;
Vector3 up = hit.normal;
Vector3 forward = Vector3.Cross(Vector3.up, up);
Vector3 finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, Quaternion.LookRotation(forward, up).eulerAngles.y + pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
pendingObject.transform.eulerAngles = finalRot;
}
This didn't work. I am assuming that I should change how I am doing the normal snapping entirely, but I have no clue how to go about this. Any help would be appreciated, even anywhere else in my script you see problems.
Answer by Junglerally · Jan 08 at 09:07 PM
FIXED: I made a new method for returning the vector3 and used the same if statement. The new method has some checks to see if the normal is positive or negative and returns different values for that. Here is the new method for anyone interested:
Vector3 GetObjectRot()
{
Vector3 finalRot;
Debug.Log(hit.normal);
previousNormal = hit.normal;
Vector3 up = hit.normal;
Vector3 forward = Vector3.Cross(Vector3.up, up);
if (hit.normal.x < 0)
{
finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z);
}
else
{
finalRot = new Vector3(Quaternion.LookRotation(forward, up).eulerAngles.x, pendingObject.transform.eulerAngles.y, Quaternion.LookRotation(forward, up).eulerAngles.z*-1);
}
return finalRot;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rotate object so part of it faces another direction 0 Answers
Get rotation from a vector direction. 1 Answer
Rotate an object so its up vector matches a sphere 1 Answer
3D nested Turret Prefab Rotation 1 Answer