PRIMERO: LO SIENTO POR MI INGLÉS!!!
Hola a todos, soy muy nuevo en Unity (5 días). Hoy hice un guión para los movimientos básicos con un cuerpo rígido, PERO los movimientos diagonales son más rápidos que los movimientos normales. Busco en Internet pero no encuentro una publicación que pueda entender.
Así que aquí mi guión. También si sabes cómo no movernos cuando saltamos, o cuando saltamos en una dirección, sigue esa dirección, dímelo. (Sé que mi inglés es terrible). Además, soy nuevo en este sitio web.
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMovements : MonoBehaviour { [SerializeField] private float walkingSpeed; [SerializeField] private float runningSpeed; [SerializeField] private float jumpForce; [SerializeField] private float jumpRaycastDistance; private Rigidbody rb; float speed; Vector3 movement; ///////////////////////////////////////////////////// void Start() { rb = GetComponent<Rigidbody>(); } ///////////////////////////////////////////////////// void Update() { Jumping(); } ///////////////////////////////////////////////////// private void FixedUpdate() { Movements(); } ///////////////////////////////////////////////////// private void Movements() { float hAxis = Input.GetAxisRaw("Horizontal"); float vAxis = Input.GetAxisRaw("Vertical"); if(Input.GetButton("Run")) { Vector3 movement = new Vector3(hAxis, 0, vAxis) * runningSpeed * Time.deltaTime; Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement); rb.MovePosition(newPosition); } else { Vector3 movement = new Vector3(hAxis, 0, vAxis) * walkingSpeed * Time.deltaTime; Vector3 newPosition = rb.position + rb.transform.TransformDirection(movement); rb.MovePosition(newPosition); } } ///////////////////////////////////////////////////// private void Jumping() { if(Input.GetButtonDown("Jump")) { if (isGrounded()) { rb.AddForce(0, jumpForce, 0, ForceMode.Impulse); } } } ///////////////////////////////////////////////////// private bool isGrounded() { return Physics.Raycast(transform.position, Vector3.down, jumpRaycastDistance); } }Necesitas sujetar tu velocidad para mantenerla igual. Mire en Vector3.ClampMagnitude()
velocity = Vector3.ClampMagnitude(velocity, _movementSpeed); velocity.y = playerVelocity.Y; // keeping your Y velocity same since you have jumping. playerVelocity = velocity; EDITAR: en tu caso debería ser algo como esto. _maxSpeed es el límite de velocidad.
private void FixedUpdate() { Movements(); var clampedVelocity = Vector3.ClampMagnitude(rb.velocity, _maxSpeed); clampedVelocity.y = rb.velocity.y; rb.velocity = clampedVelocity; }Intenta normalizar el vector. (Hay más información en los documentos de Unity, pero normalize() se asegurará de que la suma de todos los valores del vector no esté por encima de 1.