Skip to content

Instantly share code, notes, and snippets.

@leventeren
Created September 27, 2023 11:33
Show Gist options
  • Save leventeren/9c9d964ffe17d7ee2ab66586c35d4faa to your computer and use it in GitHub Desktop.
Save leventeren/9c9d964ffe17d7ee2ab66586c35d4faa to your computer and use it in GitHub Desktop.
Ref : t3ssel8r
using UnityEngine;
public class LandFacingUpward: MonoBehaviour
{
public LayerMask layerMask;
public float maxDistance = 5;
public Vector3 up = Vector3.up;
public float Kp = 100;
public float Kd = 20;
Rigidbody rb;
void Start(){
rb = GetComponent<RigidBody>();
}
void FixedUpdate(){
if (Physics.Raycast(rb.position, Physics.gravity, out RaycastHit hitInfo, maxDistance, layerMask)){
Quaternion.FromToRotation(rb.rotation * up, hitInfo.normal).ToAngleAxis(out float angle, out Vector3 axis);
Vector3 err = Mathf.Deg2Rad * angle * axis;
rb.AddTorque(Kp * err - Kd * rb.angularVelocity, ForceMode.Acceleration);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment