How to face towards vertical collider?
I have a scene setup up with a pathfinding system that uses algorithms and waypoints. The scene is setup as one collider to save on performance. It's simple. I have a squirrel character and a tree. At times I want this squirrel to climb up the tree. The pathfinding script can't calculate vertical colliders so I have to setup waypoints to connect to the original grid. It all works fine. I can click at any point on the floor and the squirrel goes to it flawlessly.
I use this snippet for it's body to rotate to the target:
Quaternion rotation = Quaternion.LookRotation(Path[Path.Count-1].GetCoordinate() + Offset - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 6);
Using this snippet when climbing the tree doesn't work so well. The squirrel tends to climb sideways rather than its belly facing the tree.
I tried raycasting to find a point in the collider and referencing its normals to rotate but half of its body goes through the tree:
if (transform.position.y > 0.2f) {
RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.forward, 1f);
foreach (RaycastHit hit in hits) {
if (hit.collider.name == "treeBlock") {
transform.rotation = Quaternion.LookRotation(hit.normal*-1f);
break;
}
}
}
I'm so close but out of ideas. Any help or suggestion would be appreciated.