- Home /
Make an animated model move/rotate along a mesh
At a very high level, what I'm trying to do is have a character which can move (animate) along the surface of any mesh, regardless of position, angle, etc.
Have 2 ideas currently:
Character moves via animation, and is rotated to match the surface by getting the normal from a raycast hit, potentially moved closer to the surface if the raycast is out of range
Character moves via animation, and force is applied to a rigidbody to 'glue' it to the surface identified by raycast hit (still would need to be rotated a la the first idea)
I've tried to implement number one, but it seems that I cannot rotate an object if the Animator is controlling it. I've tried putting my code (below) in both Update and LateUpdate, but the end result is always a horrible, flickering spasm where the Animator just undoes the rotation that I apply, and continues to move the player along the original trajectory.
Could use some pointers as to what I'm doing wrong, or perhaps a better, third idea if both of mine are flawed. Thanks!
Code:
GameObject centerOfChar = GameObject.Find("SpineMarker");
Debug.DrawRay(centerOfChar.transform.position, centerOfChar.transform.forward, Color.green);
RaycastHit hit;
Physics.Raycast(centerOfChar.transform.position, centerOfChar.transform.forward, out hit, 1f);
gameObject.transform.rotation = Quaternion.FromToRotation(Vector3.forward, hit.normal);