Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[Solved] Faux Gravity (Spherical Gravity) Movement

Discussion in 'Scripting' started by Haxxy28, Nov 2, 2019.

  1. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Firstly thank you for taking your time to read this.
    Basically I have two scripts used to create faux gravity (spherical gravity).
    One for the planet the player will be attracted to:
    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FauxGravityAttractor : MonoBehaviour
    6. {
    7.  
    8.     public float gravity = -1;
    9.     public void Attract(Transform body)
    10.     {
    11.         Vector3 gravityUp = (body.position - transform.position).normalized;
    12.         Vector3 bodyUp = body.up;
    13.  
    14.         body.GetComponent<Rigidbody>().AddForce(gravityUp * gravity);
    15.         Quaternion targetRotation = Quaternion.FromToRotation(bodyUp, gravityUp) * body.rotation;
    16.         body.rotation = Quaternion.Slerp(body.rotation, targetRotation, 50 * Time.deltaTime);
    17.  
    18.     }
    19. }
    and the other is for the object that the gravity it affecting:


    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FauxGravityBody : MonoBehaviour
    6. {
    7.     public FauxGravityAttractor attractor;
    8.     private Transform myTransform;
    9.  
    10.  
    11.     void Start()
    12.     {
    13.         GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
    14.         GetComponent<Rigidbody>().useGravity = false;
    15.         myTransform = transform;
    16.     }
    17.  
    18.  
    19.     void Update()
    20.     {
    21.         attractor.Attract(myTransform);
    22.     }
    23. }
    I was wondering how to create Player Movement as ones that I have found don't work, or make the movement look weird. Bare in mind this is for a vehicle moving around the planet, therefore it needs to turn rather than just strafe which is why:

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public Rigidbody rb;
    8.  
    9.     public float MoveSpeed= 1f;
    10.  
    11.     public float sidewaysForce = 5f;
    12.  
    13.  
    14.  
    15.    
    16.     void Update()
    17.     {
    18.         Vector3 moveDir = new Vector3(
    19.             Input.GetAxisRaw("Horizontal") * MoveSpeed * Time.deltaTime,
    20.          
    21.             Input.GetAxisRaw("Vertical") * MoveSpeed * Time.deltaTime);
    22.  
    23.      
    24.             transform.Translate(moveDir * MoveSpeed);
    25.     }
    26. }
    doesn't work. Additionally It causes the car to fly off the planet. The movement speed needs to be a variable as well as being constantly applied to the car as its not meant to stop.
    Thank you again if your read this far.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    On a spherical object, you cannot make the move direction the sameas the global up and forward. Rather, you need to make it based on the object's (local) forward and right:
    Code (CSharp):
    1. // move forward
    2. Vector3 fw = transform.forward * Input.GetAxis("Horizontal") * MoveSpeed * Time.deltaTime;
    3. // move lateral
    4. Vector3 lateral = transform.right * Input.GetAxis("Vertical") * MoveSpeed * Time.deltaTime;
    5.  
    6. // put it together
    7. transform.Translate( fw + lateral);
    Note also that you are multiplying the move vector with MoveSpeed twice, once moderated by delta time, and thenagain unmoderated in translate.
     
  3. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Thanks for the reply however I tried this and it creates a weird movement pattern, where it moves diagonally, and then stops moving and tries to push through the planet.

    Any other suggestions?
     
  4. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Is there a way to add a force based on local axis? or can that code be tweaked to work on the x and z planes rather than W making the player go upwards/downwards?
     
  5. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19