Hello everyone,
I’m trying to make a planetary gravity system for a VR Player so I can’t use rigidbodies (on the player) so I made my own but this caused a problem with my planetary gravity where it automatically moves the player to the top of a planet even though it’s supposed to just let the player go where ever they want on the planet.
Gravity Ctrl (this goes on the player):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityCtrl : MonoBehaviour
{
public float rotationSpeed = 20f;
[HideInInspector]
public GravityOrbit gravity;
private PlayerRigidbodyVR rb;
void Awake()
{
rb = GetComponent<PlayerRigidbodyVR>();
}
void FixedUpdate()
{
if(gravity)
{
Vector3 gravityUp = Vector3.zero;
gravityUp = (transform.position - gravity.transform.position).normalized;
Vector3 localUp = transform.up;
Quaternion targetRot = Quaternion.FromToRotation(localUp, gravityUp) * transform.rotation;
transform.rotation = targetRot;
//transform.up = Vector3.Lerp(transform.up, gravityUp, rotationSpeed * Time.deltaTime);
rb.AddForceVR((-gravityUp * gravity.gravity) * rb.mass);
//rb.AddForceRequireGndVR((-gravityUp * gravity.gravity) * rb.mass, false);
}
}
}
Gravity Orbit (this goes on a planet):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityOrbit : MonoBehaviour
{
public float gravity = 2;
private void OnTriggerEnter(Collider other)
{
if(other.GetComponent<GravityCtrl>())
{
other.GetComponent<GravityCtrl>().gravity = GetComponent<GravityOrbit>();
}
}
}
And of course the AddForceVR in my PlayerRigidbodyVR script:
public void AddForceVR(Vector3 force)
{
character.Move(Vector3.right * force.x * Time.deltaTime);
character.Move(Vector3.up * force.y * Time.deltaTime);
character.Move(Vector3.forward * force.z * Time.deltaTime);
}
Thanks in advance!