Created
September 27, 2023 11:33
-
-
Save leventeren/9c9d964ffe17d7ee2ab66586c35d4faa to your computer and use it in GitHub Desktop.
Ref : t3ssel8r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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