Planetarny kontroler odtwarzacza oparty na Rigidbody dla Unity
Podczas tworzenia kontrolera odtwarzacza grawitacja jest zwykle przykładana tylko w jednym kierunku, czyli w dół.
Ale co z grawitacją z punktem centralnym? To zadanie dla wędrowca planetarnego.
Chodzik planetarny to rodzaj kontrolera, który pozwala graczowi chodzić po kulistym obiekcie (podobnie jak planety), przy czym środek ciężkości znajduje się w środku kuli.
Kroki
Poniżej znajdują się kroki potrzebne do zbudowania planetarnego chodzika o sztywnym ciele, ze środkowym punktem ciężkości w Unity:
- Otwórz Scenę ze swoim okrągłym poziomem (w moim przypadku mam niestandardowy model planety i niestandardowy Skybox w Scenie)
- Utwórz nowy skrypt, nazwij go "SC_RigidbodyWalker" i wklej w nim poniższy kod:
SC_RigidbodyWalker.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class SC_RigidbodyWalker : MonoBehaviour
{
public float speed = 5.0f;
public bool canJump = true;
public float jumpHeight = 2.0f;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 60.0f;
bool grounded = false;
Rigidbody r;
Vector2 rotation = Vector2.zero;
float maxVelocityChange = 10.0f;
void Awake()
{
r = GetComponent<Rigidbody>();
r.freezeRotation = true;
r.useGravity = false;
r.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
rotation.y = transform.eulerAngles.y;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// Player and Camera rotation
rotation.x += -Input.GetAxis("Mouse Y") * lookSpeed;
rotation.x = Mathf.Clamp(rotation.x, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotation.x, 0, 0);
Quaternion localRotation = Quaternion.Euler(0f, Input.GetAxis("Mouse X") * lookSpeed, 0f);
transform.rotation = transform.rotation * localRotation;
}
void FixedUpdate()
{
if (grounded)
{
// Calculate how fast we should be moving
Vector3 forwardDir = Vector3.Cross(transform.up, -playerCamera.transform.right).normalized;
Vector3 rightDir = Vector3.Cross(transform.up, playerCamera.transform.forward).normalized;
Vector3 targetVelocity = (forwardDir * Input.GetAxis("Vertical") + rightDir * Input.GetAxis("Horizontal")) * speed;
Vector3 velocity = transform.InverseTransformDirection(r.velocity);
velocity.y = 0;
velocity = transform.TransformDirection(velocity);
Vector3 velocityChange = transform.InverseTransformDirection(targetVelocity - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
velocityChange = transform.TransformDirection(velocityChange);
r.AddForce(velocityChange, ForceMode.VelocityChange);
if (Input.GetButton("Jump") && canJump)
{
r.AddForce(transform.up * jumpHeight, ForceMode.VelocityChange);
}
}
grounded = false;
}
void OnCollisionStay()
{
grounded = true;
}
}
- Utwórz nowy skrypt, nazwij go "SC_PlanetGravity" i wklej w nim poniższy kod:
SC_PlanetGravity.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SC_PlanetGravity : MonoBehaviour
{
public Transform planet;
public bool alignToPlanet = true;
float gravityConstant = 9.8f;
Rigidbody r;
void Start()
{
r = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
Vector3 toCenter = planet.position - transform.position;
toCenter.Normalize();
r.AddForce(toCenter * gravityConstant, ForceMode.Acceleration);
if (alignToPlanet)
{
Quaternion q = Quaternion.FromToRotation(transform.up, -toCenter);
q = q * transform.rotation;
transform.rotation = Quaternion.Slerp(transform.rotation, q, 1);
}
}
}
- Utwórz nowy obiekt GameObject i wywołaj go "Player"
- Utwórz nową kapsułę, przesuń ją do obiektu "Player" i zmień jej położenie na (0, 1, 0)
- Usuń komponent Capsule Collider z kapsuły
- Przesuń kamerę główną do obiektu "Player" i zmień jej położenie na (0, 1,64, 0)
- Dołącz skrypt SC_RigidbodyWalker do obiektu "Player" (zauważysz, że doda on dodatkowe komponenty, takie jak Rigidbody i Capsule Collider).
- Zmień wysokość zderzacza kapsułek na 2 i środek na (0, 1, 0)
- Przypisz kamerę główną do zmiennej kamery gracza w SC_RigidbodyWalker
- Na koniec dołącz skrypt SC_PlanetGravity do obiektu "Player" i przypisz model swojej planety do zmiennej Planet
Naciśnij przycisk Play i obserwuj, jak odtwarzacz ustawia się na powierzchni planety: