Нежелательное вращение. Когда я прыгаю или двигаюсь в сторону, объект игрока вращается сам по себе. Если я заблокирую ось Y в ограничениях Rigidbody, мой сценарий просмотра с помощью мыши перестанет работать правильно.
Я пробовал:
Если я зафиксирую камеру на оси Y в Unity, камера будет двигаться так, как если бы она была прикреплена, и похоже, что у нее падает FPS.
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControl : MonoBehaviour
{
[SerializeField] Camera p_camera;
[SerializeField] InputActionAsset p_inputMap;
//Input//
InputAction m_lookAction;
InputAction m_moveAction;
InputAction m_jumpAction;
Vector2 moveActions;
Vector2 lookActions;
private Rigidbody rb;
private float xRotation;
private float yRotation;
[SerializeField] float sensitivity;
[SerializeField] float maxSpeed;
[SerializeField] float airAccel;
[SerializeField] float groundAccel;
[SerializeField] float deAccel;
[SerializeField] float jumpForce;
[SerializeField] bool isGround;
private void Awake()
{
rb = GetComponent();
//İmleç ayarları.
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//Input maping
m_lookAction = p_inputMap.FindAction("Player/Look");
m_moveAction = p_inputMap.FindAction("Player/Move");
m_jumpAction = p_inputMap.FindAction("Player/Jump");
m_lookAction.Enable();
m_moveAction.Enable();
m_jumpAction.Enable();
}
// Her framede çalışır
void Update()
{
Look();
}
//Her fizik saniyesindi çalışıyor
private void FixedUpdate()
{
Move();
Jump();
}
//Gorund check yapıldığı yer
private void OnTriggerStay(Collider other)
{
isGround = true;
}
private void OnTriggerExit(Collider other)
{
isGround = false;
}
void Look()
{
//Fare inputlarını güncellemem
lookActions = m_lookAction.ReadValue();
//Mouse delta değerleri okuma
float mouseX = lookActions.x * sensitivity * Time.deltaTime;
float mouseY = lookActions.y * sensitivity * Time.deltaTime;
//Mouse yukarı-aşağı
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -89f, 89f);
//Mouse sağa-sola
yRotation = mouseX;
//Kamerayı çevirme yukarı-aşağı
p_camera.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
//Karakteri sağa-sola çevirme
transform.Rotate(Vector3.up * mouseX);
}
void Move()
{
// TODO: Bunnyhop,acseleriti,airacseleriti ve deaceleriti ekle.
moveActions = m_moveAction.ReadValue();
float currentAccel = isGround ? groundAccel : airAccel;
Vector3 moveDir = transform.forward * moveActions.y + transform.right * moveActions.x;
Vector3 targetVelocity = moveDir * maxSpeed;
Vector3 currentHorizontalVelocity = new(rb.linearVelocity.x, 0, rb.linearVelocity.z);
Vector3 newHorizontalVelocity = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, currentAccel);
Vector3 newHorizontalDeAccel = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, deAccel);
float walkSpeed = newHorizontalVelocity.magnitude;
Debug.Log(walkSpeed);
if (moveActions.magnitude > 0)
{
rb.linearVelocity = new Vector3(newHorizontalVelocity.x, rb.linearVelocity.y, newHorizontalVelocity.z);
}
else
{
rb.linearVelocity = new Vector3(newHorizontalDeAccel.x, rb.linearVelocity.y, newHorizontalDeAccel.z);
}
}
void Jump()
{
if (isGround)
{
if (m_jumpAction.IsPressed())
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z);
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... axis-issue
Проблема с осью вращения твердого тела персонажа FPS ⇐ C#
Место общения программистов C#
-
Anonymous
1767871415
Anonymous
[b]Нежелательное вращение.[/b] Когда я прыгаю или двигаюсь в сторону, объект игрока вращается сам по себе. Если я заблокирую ось Y в ограничениях Rigidbody, мой сценарий просмотра с помощью мыши перестанет работать правильно.
[b]Я пробовал:[/b]
Если я зафиксирую камеру на оси Y в Unity, камера будет двигаться так, как если бы она была прикреплена, и похоже, что у нее падает FPS.
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerControl : MonoBehaviour
{
[SerializeField] Camera p_camera;
[SerializeField] InputActionAsset p_inputMap;
//Input//
InputAction m_lookAction;
InputAction m_moveAction;
InputAction m_jumpAction;
Vector2 moveActions;
Vector2 lookActions;
private Rigidbody rb;
private float xRotation;
private float yRotation;
[SerializeField] float sensitivity;
[SerializeField] float maxSpeed;
[SerializeField] float airAccel;
[SerializeField] float groundAccel;
[SerializeField] float deAccel;
[SerializeField] float jumpForce;
[SerializeField] bool isGround;
private void Awake()
{
rb = GetComponent();
//İmleç ayarları.
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
//Input maping
m_lookAction = p_inputMap.FindAction("Player/Look");
m_moveAction = p_inputMap.FindAction("Player/Move");
m_jumpAction = p_inputMap.FindAction("Player/Jump");
m_lookAction.Enable();
m_moveAction.Enable();
m_jumpAction.Enable();
}
// Her framede çalışır
void Update()
{
Look();
}
//Her fizik saniyesindi çalışıyor
private void FixedUpdate()
{
Move();
Jump();
}
//Gorund check yapıldığı yer
private void OnTriggerStay(Collider other)
{
isGround = true;
}
private void OnTriggerExit(Collider other)
{
isGround = false;
}
void Look()
{
//Fare inputlarını güncellemem
lookActions = m_lookAction.ReadValue();
//Mouse delta değerleri okuma
float mouseX = lookActions.x * sensitivity * Time.deltaTime;
float mouseY = lookActions.y * sensitivity * Time.deltaTime;
//Mouse yukarı-aşağı
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -89f, 89f);
//Mouse sağa-sola
yRotation = mouseX;
//Kamerayı çevirme yukarı-aşağı
p_camera.transform.localRotation = Quaternion.Euler(xRotation, 0, 0);
//Karakteri sağa-sola çevirme
transform.Rotate(Vector3.up * mouseX);
}
void Move()
{
// TODO: Bunnyhop,acseleriti,airacseleriti ve deaceleriti ekle.
moveActions = m_moveAction.ReadValue();
float currentAccel = isGround ? groundAccel : airAccel;
Vector3 moveDir = transform.forward * moveActions.y + transform.right * moveActions.x;
Vector3 targetVelocity = moveDir * maxSpeed;
Vector3 currentHorizontalVelocity = new(rb.linearVelocity.x, 0, rb.linearVelocity.z);
Vector3 newHorizontalVelocity = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, currentAccel);
Vector3 newHorizontalDeAccel = Vector3.MoveTowards(currentHorizontalVelocity, targetVelocity, deAccel);
float walkSpeed = newHorizontalVelocity.magnitude;
Debug.Log(walkSpeed);
if (moveActions.magnitude > 0)
{
rb.linearVelocity = new Vector3(newHorizontalVelocity.x, rb.linearVelocity.y, newHorizontalVelocity.z);
}
else
{
rb.linearVelocity = new Vector3(newHorizontalDeAccel.x, rb.linearVelocity.y, newHorizontalDeAccel.z);
}
}
void Jump()
{
if (isGround)
{
if (m_jumpAction.IsPressed())
{
rb.linearVelocity = new Vector3(rb.linearVelocity.x, jumpForce, rb.linearVelocity.z);
}
}
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79860291/fps-character-rigidbody-rotation-axis-issue[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия