Перемещение в нулевой гравитации
Подскажите пожалуйста как реализовать перемещение в нулевой гравитации? В переменную goDirection должно записываться направление движения игрока, которое зависит от horAxis, vertAxis и cameraHolder.transform.rotation
using UnityEngine;
public class Movement : MonoBehaviour
{
public Transform cameraHolder;
public float mouseSensitivity = 2f;
public float upLimit = -70;
public float downLimit = 70;
private float horAxis;
private float vertAxis;
private Rigidbody playerRB;
void Start()
{
playerRB = GetComponent<Rigidbody>();
}
private void Awake()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void FixedUpdate()
{
Move();
Rotate();
}
private void Move()
{
horAxis = Input.GetAxisRaw("Horizontal");
vertAxis = Input.GetAxisRaw("Vertical");
Vector3 goDirection = new Vector3(); // здесь надо указать направления движения
playerRB.AddForce(goDirection, ForceMode.Acceleration);
}
private void Rotate()
{
float horizontalRotation = Input.GetAxis("Mouse X");
float verticalRotation = Input.GetAxis("Mouse Y");
transform.Rotate(0, horizontalRotation * mouseSensitivity, 0);
cameraHolder.Rotate(-verticalRotation * mouseSensitivity, 0, 0);
Vector3 currentRotation = cameraHolder.localEulerAngles;
if (currentRotation.x > 180) currentRotation.x -= 360;
currentRotation.x = Mathf.Clamp(currentRotation.x, upLimit, downLimit);
currentRotation.z = 0;
cameraHolder.localRotation = Quaternion.Euler(currentRotation);
}
}
Ответы (1 шт):
Автор решения: Overlord
→ Ссылка
Это один из вариантов:
private void Move()
{
horAxis = Input.GetAxisRaw("Horizontal");
vertAxis = Input.GetAxisRaw("Vertical");
Vector3 goDirection = new Vector3();
if (vertAxis > 0)
{
goDirection = transform.forward;
}
if (vertAxis < 0)
{
goDirection = -1 * transform.forward;
}
if (horAxis > 0)
{
goDirection = transform.right;
}
if (horAxis < 0)
{
goDirection = -1 * transform.right;
}
playerRB.AddForce(goDirection, ForceMode.Acceleration);
}
так-же можно поставить макс. скорость:
Vector3 velocity = playerRB.velocity;
if (velocity.magnitude > maxSpeed)
{
playerRB.velocity = playerRB.velocity.normalized * maxSpeed;
}
playerRB.AddForce(goDirection, ForceMode.Acceleration);