Created
October 4, 2020 15:41
-
-
Save vallentiin/1a7d747e811e54e0389fe23b5d18e275 to your computer and use it in GitHub Desktop.
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class FauxGravityAttractor : MonoBehaviour | |
{ | |
public float gravity = -10f; | |
public float slerpSpeed = 50f; | |
public void Attract (Transform body) | |
{ | |
Vector3 gravityUp = (body.position - transform.position).normalized; | |
Vector3 bodyUp = body.up; | |
body.GetComponent<Rigidbody>().AddForce(gravityUp * gravity); | |
Quaternion targetRotation = Quaternion.FromToRotation(bodyUp, gravityUp) * body.rotation; | |
body.rotation = Quaternion.Slerp(body.rotation, targetRotation, slerpSpeed * Time.deltaTime); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment