- Home /
Aligning an object with surface normal
Ok, I know how this sounds, but it isn't like that :D. I know there's many questions regarding this, but I believe my problem is a bit different and after working for hours my brain is fried, hence resists to my attempts of programming. I have a character, with this hierarchy
- Character
Visual mesh
It's a top-down shooter, so the character is contrantly spinning around, trying to look at the mouse, which makes the visual mesh spin too. Now, normally, I'd use
VisualMesh.localRotation = Quaternion.FromToRotation(VisualMesh.up, hit.normal);
But this doesn't work, because as soon as I rotate the character around, it stops being aligned.
So, my question is, how would I go about aligning the visual object with the ground, while making it keep it's parents Y rotation.
Pictures
Character's y rotation set to 0
Character's y rotation set to 180
--David
Answer by DavidDebnar · Oct 19, 2013 at 09:50 PM
Thanks Jamora! I didn't know you could assign transform.up and it almost solved my problem. All I had to do then was apply the Y rotation.
// assigns the raycast's normal direction to the VisualMesh' up directio. this makes it look 'from the surface'
// then use some lerping, to smooth it all out
VisualMesh.up = Vector3.Lerp(VisualMesh.up, hit.normal, Time.deltaTime * 10);
// this part makes sure the Y rotation is properly preserved from the parent gameObject as well
VisualMesh.RotateAround(VisualMesh.position, VisualMesh.up, transform.localEulerAngles.y);
--David