Ich mache einen 3D -Plattformer in Einheit, wo Sie sich bewegen, aussehen und springen können. Aber aus irgendeinem Grund ändert sich die Position des Spielers, wenn ich nichts drücke, ständig inkrementell, anstatt nur eingestellt zu bleiben. Hier ist mein Code: < /p>
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBehavior : MonoBehaviour
{
public Rigidbody rb;
public InputAction move, jump, look;
private Vector2 keyMove, inputLook;
private bool keyJump;
public float moveSpeed = 8f;
public float jumpHeight = 12f;
public float lookSpeed = 2f, pitch = 0f, yaw = 0f;
private void OnEnable()
{
move.Enable();
jump.Enable();
look.Enable();
}
private void OnDisable()
{
move.Disable();
jump.Disable();
look.Disable();
}
private void Start()
{
rb = GetComponent();
}
private void Update()
{
keyMove = move.ReadValue();
if (keyMove.magnitude < 0.2f) { keyMove = Vector2.zero; }
Vector3 moveDirection = transform.forward * keyMove.y + transform.right * keyMove.x;
moveDirection.y = 0f;
rb.linearVelocity = new Vector3(
moveDirection.x * moveSpeed,
rb.linearVelocity.y,
moveDirection.z * moveSpeed
);
keyJump = jump.ReadValue() > 0.5f;
if (keyJump)
{
rb.linearVelocity = new Vector3(
rb.linearVelocity.x,
jumpHeight,
rb.linearVelocity.z
);
}
inputLook = look.ReadValue();
if (inputLook.magnitude < 0.05f)
{
inputLook = Vector2.zero;
}
// This is necessary because the mouse input detects the change in mousepos.
// And we need to accumulate (integrate) these changes to get the actual θ value we want
yaw += inputLook.x * lookSpeed;
pitch -= inputLook.y * lookSpeed;
transform.rotation = Quaternion.Euler(pitch, yaw, transform.rotati
< /code>
Ich habe versucht, die Geschwindigkeit manuell auf Null festzulegen, die Bewegung Eingabe auf Null. Ich habe versucht zu sehen, ob es sich um eine Übersetzungssache der Rotationsposition handelt. Es ist nicht. Ich habe versucht, eine lineare Dämpfung einzustellen, ein benutzerdefiniertes Physikmaterial mit mehr Reibung. Aber bisher hat nichts funktioniert. Es nur wenige Zentimeter ständig nach vorne. Können Sie mir bitte bei diesem
Problem helfen?
Ich mache einen 3D -Plattformer in Einheit, wo Sie sich bewegen, aussehen und springen können. Aber aus irgendeinem Grund ändert sich die Position des Spielers, wenn ich nichts drücke, ständig inkrementell, anstatt nur eingestellt zu bleiben. Hier ist mein Code: < /p>
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerBehavior : MonoBehaviour
{
public Rigidbody rb;
public InputAction move, jump, look;
private Vector2 keyMove, inputLook;
private bool keyJump;
public float moveSpeed = 8f;
public float jumpHeight = 12f;
public float lookSpeed = 2f, pitch = 0f, yaw = 0f;
private void OnEnable()
{
move.Enable();
jump.Enable();
look.Enable();
}
private void OnDisable()
{
move.Disable();
jump.Disable();
look.Disable();
}
private void Start()
{
rb = GetComponent();
}
private void Update()
{
keyMove = move.ReadValue();
if (keyMove.magnitude < 0.2f) { keyMove = Vector2.zero; }
Vector3 moveDirection = transform.forward * keyMove.y + transform.right * keyMove.x;
moveDirection.y = 0f;
rb.linearVelocity = new Vector3(
moveDirection.x * moveSpeed,
rb.linearVelocity.y,
moveDirection.z * moveSpeed
);
keyJump = jump.ReadValue() > 0.5f;
if (keyJump)
{
rb.linearVelocity = new Vector3(
rb.linearVelocity.x,
jumpHeight,
rb.linearVelocity.z
);
}
inputLook = look.ReadValue();
if (inputLook.magnitude < 0.05f)
{
inputLook = Vector2.zero;
}
// This is necessary because the mouse input detects the change in mousepos.
// And we need to accumulate (integrate) these changes to get the actual θ value we want
yaw += inputLook.x * lookSpeed;
pitch -= inputLook.y * lookSpeed;
transform.rotation = Quaternion.Euler(pitch, yaw, transform.rotati
< /code>
Ich habe versucht, die Geschwindigkeit manuell auf Null festzulegen, die Bewegung Eingabe auf Null. Ich habe versucht zu sehen, ob es sich um eine Übersetzungssache der Rotationsposition handelt. Es ist nicht. Ich habe versucht, eine lineare Dämpfung einzustellen, ein benutzerdefiniertes Physikmaterial mit mehr Reibung. Aber bisher hat nichts funktioniert. Es nur wenige Zentimeter ständig nach vorne. Können Sie mir bitte bei diesem [url=viewtopic.php?t=26065]Problem[/url] helfen?