Чтобы заставить вашего игрока двигаться в направлении вашего вращения в Unity, вы можете использовать различные методы. Вот несколько примеров:
Метод 1: использование Transform.forward
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
// Get the rotation of the player
Quaternion rotation = transform.rotation;
// Get the forward direction based on the rotation
Vector3 direction = rotation * Vector3.forward;
// Normalize the direction to maintain consistent speed
direction.Normalize();
// Apply movement to the player
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
}
}
Метод 2: использование углов Эйлера
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
// Get the rotation of the player in Euler angles
Vector3 rotation = transform.eulerAngles;
// Calculate the forward direction based on the rotation
Vector3 direction = Quaternion.Euler(rotation) * Vector3.forward;
// Normalize the direction to maintain consistent speed
direction.Normalize();
// Apply movement to the player
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
}
}
Метод 3: использование Transform.rotation и Transform.TransformDirection
public class PlayerController : MonoBehaviour
{
public float speed = 5f;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
// Get the rotation of the player
Quaternion rotation = transform.rotation;
// Get the forward direction based on the rotation
Vector3 direction = rotation * Vector3.forward;
// Transform the direction from local space to world space
direction = transform.TransformDirection(direction);
// Normalize the direction to maintain consistent speed
direction.Normalize();
// Apply movement to the player
rb.MovePosition(transform.position + direction * speed * Time.deltaTime);
}
}