Anonymous
Почему объекты дергаются, когда я двигаюсь, а камера вращается?
Сообщение
Anonymous » 13 окт 2024, 16:02
Я начал разработку сценария fps и проследил руководство, чтобы помочь со сценарием. Теперь все работает отлично, но единственная проблема заключается в том, что любые объекты в представлении заикаются, когда игрок одновременно движется и вращается.
Сценарий для движения игрока и вращения камеры приведен ниже.
Может быть кто-нибудь подскажет мне правильное направление?
Спасибо.
{
Код: Выделить всё
public Transform cam;
public Rigidbody rb;
public float camRotationSpeed = 5f;
public float camMinimumY = -60f;
public float camMaximumY = 75f;
public float rotationSmoothSpeed = 10f;
public float walkSpeed = 9f;
public float runSpeed = 14f;
public float maxSpeed = 20f;
public float jumpPower = 30f;
public float extraGravity = 45;
float bodyRotationX;
float camRotationY;
Vector3 directionIntentX;
Vector3 directionIntentY;
float speed;
public bool grounded;
void Update()
{
LookRotation();
Movement();
ExtraGravity();
GroundCheck();
if(grounded && Input.GetButtonDown("Jump"))
{
Jump();
}
}
void LookRotation()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed;
camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed;
camRotationY = Mathf.Clamp(camRotationY, camMinimumY, camMaximumY);
Quaternion camTargetRotation = Quaternion.Euler(-camRotationY, 0, 0);
Quaternion bodyTargetRotation = Quaternion.Euler(0, bodyRotationX, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, bodyTargetRotation, Time.deltaTime * rotationSmoothSpeed);
cam.localRotation = Quaternion.Lerp(cam.localRotation, camTargetRotation, Time.deltaTime * rotationSmoothSpeed);
}
void Movement()
{
directionIntentX = cam.right;
directionIntentX.y = 0;
directionIntentX.Normalize();
directionIntentY = cam.forward;
directionIntentY.y = 0;
directionIntentY.Normalize();
rb.velocity = directionIntentY * Input.GetAxis("Vertical") * speed + directionIntentX * Input.GetAxis("Horizontal") * speed + Vector3.up * rb.velocity.y;
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
if (Input.GetKey(KeyCode.LeftShift))
{
speed = runSpeed;
}
if (!Input.GetKey(KeyCode.LeftShift))
{
speed = walkSpeed;
}
}
void ExtraGravity()
{
rb.AddForce(Vector3.down * extraGravity);
}
void GroundCheck()
{
RaycastHit groundHit;
grounded = Physics.Raycast(transform.position, -transform.up, out groundHit, 1.25f);
}
void Jump()
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
}
}
Подробнее здесь:
https://stackoverflow.com/questions/638 ... s-rotating
1728824568
Anonymous
Я начал разработку сценария fps и проследил руководство, чтобы помочь со сценарием. Теперь все работает отлично, но единственная проблема заключается в том, что любые объекты в представлении заикаются, когда игрок одновременно движется и вращается. Сценарий для движения игрока и вращения камеры приведен ниже. Может быть кто-нибудь подскажет мне правильное направление? Спасибо. { [code]public Transform cam; public Rigidbody rb; public float camRotationSpeed = 5f; public float camMinimumY = -60f; public float camMaximumY = 75f; public float rotationSmoothSpeed = 10f; public float walkSpeed = 9f; public float runSpeed = 14f; public float maxSpeed = 20f; public float jumpPower = 30f; public float extraGravity = 45; float bodyRotationX; float camRotationY; Vector3 directionIntentX; Vector3 directionIntentY; float speed; public bool grounded; void Update() { LookRotation(); Movement(); ExtraGravity(); GroundCheck(); if(grounded && Input.GetButtonDown("Jump")) { Jump(); } } void LookRotation() { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed; camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed; camRotationY = Mathf.Clamp(camRotationY, camMinimumY, camMaximumY); Quaternion camTargetRotation = Quaternion.Euler(-camRotationY, 0, 0); Quaternion bodyTargetRotation = Quaternion.Euler(0, bodyRotationX, 0); transform.rotation = Quaternion.Lerp(transform.rotation, bodyTargetRotation, Time.deltaTime * rotationSmoothSpeed); cam.localRotation = Quaternion.Lerp(cam.localRotation, camTargetRotation, Time.deltaTime * rotationSmoothSpeed); } void Movement() { directionIntentX = cam.right; directionIntentX.y = 0; directionIntentX.Normalize(); directionIntentY = cam.forward; directionIntentY.y = 0; directionIntentY.Normalize(); rb.velocity = directionIntentY * Input.GetAxis("Vertical") * speed + directionIntentX * Input.GetAxis("Horizontal") * speed + Vector3.up * rb.velocity.y; rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed); if (Input.GetKey(KeyCode.LeftShift)) { speed = runSpeed; } if (!Input.GetKey(KeyCode.LeftShift)) { speed = walkSpeed; } } void ExtraGravity() { rb.AddForce(Vector3.down * extraGravity); } void GroundCheck() { RaycastHit groundHit; grounded = Physics.Raycast(transform.position, -transform.up, out groundHit, 1.25f); } void Jump() { rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse); } [/code] } Подробнее здесь: [url]https://stackoverflow.com/questions/63837747/why-do-objects-stutter-while-i-move-and-the-camera-is-rotating[/url]