Good day community!
I got this script attached to the player:
using UnityEngine;
public class SurfaceAlignment : MonoBehaviour {
bool allowAlign = true;
float alignSpeed = 10f;
void Start () {
}
void FixedUpdate () {
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Planet"))
{
float atmosthereRadius = go.GetComponent<Planet>().atmosphere;
float xDist = go.transform.position[0] - transform.position[0];
float yDist = go.transform.position[1] - transform.position[1];
float zDist = go.transform.position[2] - transform.position[2];
float distSquared = Mathf.Pow(xDist, 2) + Mathf.Pow(yDist, 2) + Mathf.Pow(zDist, 2);
float dist = Mathf.Sqrt(distSquared);
if (allowAlign)
{
if (dist <= atmosthereRadius)
{
Vector3 targetPoint = go.transform.position - transform.position;
Quaternion targetRotation = Quaternion.LookRotation(targetPoint);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * alignSpeed);
}
else
{
Vector3 targetPoint = new Vector3(0f, 0f, 0f);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * alignSpeed);
}
}
}
}
}
What this code does is that when the player aproaches a sphere with the tag “Planet” in a range, it’s mesh is aligned to the vector from the center of the sphere to the player position, but problem here is that instead of landing on feet, the player lands on his belly, this means that instead of being aligned over the Y axis, the playes is aligned over the Z Axis. I just do not have enough brain to fix this.