- Home /
Problem snapping object to different normals, but maintaining x axis orientation
I'm working on an editor, where the player can move an object around the editor, snapping it to surface normal. The player can also rotate the object around its X axis. Whenever I try and snap it to the surface normal it matchs all the axis of the normal and I lose the facing that it had before the snap. I've tried a bunch of different things and I've had limited success. I'm obviously missing something.
tempX=targetObject.transform.localRotation.eulerAngles.x;
targetObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitNormal);
targetObject.transform.Rotate(Vector3.up * -tempX);
targetObject.transform.position = mInputManager.hit.point;
I fixed your code, next time be sure and format the code, by selecting it and click the "010101" button, or just indent it four spaces...
so you do you not want to randomly rotate but rotate the nearest face of the object then?
(his problem is basically I THIN$$anonymous$$ that he ends up with an object snapping to have it with a face parallel to another. but to do so he is basically just mimicking the object and so he completely flips objects all over. He wants the $$anonymous$$imum rotation needed to make an object have a face parallel to another) I THIN$$anonymous$$.
So if I click on each of these points in turn, I want to snap the object to the ground and align the local y and z of the object to the normal of the surface and leave the X axis the same as it was.
is this for like a character to run up walls and stuff?
basically i'm thinking your actually wanting to keep the y axis the same not the other one.
x axis rotation would literally rotation the tip of the arrow through the ground or wall or whatever. rotating left and right is actually a rotaiton of the Y axis not the X.
Answer by whydoidoit · Mar 27, 2013 at 09:13 AM
How about this?
targetObject.transform.rotation *= Quaternion.FromToRotation(targetObject.transform.up, hitNormal);
NICE! (it will need to be targetObject.transform.up though..)
targetObject.transform.rotation *= Quaternion.FromToRotation(targetObject.transform.up, hitNormal);
yep it works as long as you never want to knock an object over on it's side and have it work. (I was trying to help him get a more robust solution that doesnt force you to always have objects upright, but it is quite a bit more work to do so, so perhaps just having multiple models with different "up" vectors is easier for him.
you have a good idea, although I discounted that possibility off the bat, since it wasn't actually in the question
an easier way to account for an off-kilter object would be to parent it to an empty object that has the correct orientation :)
let me suggest again, this will be $$anonymous$$UCH EASIER if you simply parent your object to an empty.. you can apply the x rotation to the child, then the rotation will simply stay the same, problem solved
Your answer
Follow this Question
Related Questions
Problem snapping object to different normals, but maintaining x axis orientation 0 Answers
Rotation with a slider 2 Answers
Is there no way to get reliable angles from a rigidbody? 2 Answers
I am having a weird issue with Quaternion.FromToRotation 2 Answers
How make child object rotate inverse to its parents rotation ? 1 Answer