Skip to content

Instantly share code, notes, and snippets.

@niuage
Last active May 6, 2020 14:05
Show Gist options
  • Save niuage/096b92b62d0c485b991137391e5cd245 to your computer and use it in GitHub Desktop.
Save niuage/096b92b62d0c485b991137391e5cd245 to your computer and use it in GitHub Desktop.
// target is the player
// transform is the spider
Vector3 orientation = GetTerrainNormal();
Vector3 directionToTarget = (target.position - transform.position).Y(0);
float d = Vector3.Dot(directionToTarget, orientation);
directionToTarget -= d * orientation;
if (directionToTarget.sqrMagnitude > 0.00001f) {
directionToTarget.Normalize();
Quaternion rotationNeeded = Quaternion.LookRotation(directionToTarget, orientation);
transform.rotation = Quaternion.RotateTowards(
transform.rotation,
rotationNeeded,
xRotationSpeed * Time.deltaTime
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment