Skip to content

Instantly share code, notes, and snippets.

@vallentiin
Created October 4, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vallentiin/1a7d747e811e54e0389fe23b5d18e275 to your computer and use it in GitHub Desktop.
Save vallentiin/1a7d747e811e54e0389fe23b5d18e275 to your computer and use it in GitHub Desktop.
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