
Move the Main Camera inside "Player" object and change its position to (0, 1.64, 0). Remove Capsule Collider component from the Capsule. Create a new Capsule, move it inside "Player" object and change its position to (0, 1, 0). Create a new GameObject and call it "Player". Transform.rotation = Quaternion.Slerp(transform.rotation, q, 1) Quaternion q = Quaternion.FromToRotation(transform.up, -toCenter) R.AddForce(toCenter * gravit圜onstant, ForceMode.Acceleration) Vector3 toCenter = planet.position - transform.position Public class SC_PlanetGravity : MonoBehaviour SC_PlanetGravity.cs using System.Collections #Unity 3d gravity code#
Create a new script, call it "SC_PlanetGravity" and paste the code below inside it:. R.AddForce(transform.up * jumpHeight, ForceMode.Velocit圜hange) R.AddForce(velocit圜hange, ForceMode.Velocit圜hange) Velocit圜hange = transform.TransformDirection(velocit圜hange) Velocit圜hange.z = Mathf.Clamp(velocit圜hange.z, -maxVelocit圜hange, maxVelocit圜hange) Velocit圜hange.x = Mathf.Clamp(velocit圜hange.x, -maxVelocit圜hange, maxVelocit圜hange) Vector3 velocit圜hange = transform.InverseTransformDirection(targetVelocity - velocity) Velocity = transform.TransformDirection(velocity) Vector3 velocity = transform.InverseTransformDirection(r.velocity) Vector3 targetVelocity = (forwardDir * Input.GetAxis("Vertical") + rightDir * Input.GetAxis("Horizontal")) * speed Vector3 rightDir = Vector3.Cross(transform.up, ).normalized Vector3 forwardDir = Vector3.Cross(transform.up, ).normalized Calculate how fast we should be moving Transform.rotation = transform.rotation * localRotation Quaternion localRotation = Quaternion.Euler(0f, Input.GetAxis("Mouse X") * lookSpeed, 0f)
Rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit) Rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed R.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic Ĭursor.lockState = CursorLockMode.Locked Public class SC_RigidbodyWalker : MonoBehaviour SC_RigidbodyWalker.cs using System.Collections Create a new script, call it "SC_RigidbodyWalker" and paste the code below inside it:.
Open the Scene with your circular level (in my case I have a custom-made planet model and a custom Skybox from here).Stepsīelow are the steps to make a planetary rigidbody walker, with a central point of gravity: When creating a Player Controller, the gravity is usually applied in just one direction, which is down.īut what about gravity with a central point? This is a job for the planetary walker.Ī planetary walker is a type of controller that allows the player to walk on a spherical object (just like the planets are giant spheres), with the center of gravity being in a center of the sphere.