Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

244
Vistas
Diagonal movements are faster than normal movements

FIRST : SORRY FOR MY ENGLISH !!!

Hello everyone, i'm very new into Unity (5 days). Today i've make a script for the basics movements with a rigid-bodie, BUT the diagonal movements are faster than the normal movements.. I search on Internet but I don't find a post that i can understand.

So here my script. Also if you know how to not move when we jump, or when we jump into a direction, it follow this direction, tell me. (I know my english is terrible.) Also, i'm new into this website.

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);
    }
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

You need to clamp your velocity in order to keep it same. Look into Vector3.ClampMagnitude()

velocity = Vector3.ClampMagnitude(velocity, _movementSpeed);
            velocity.y = playerVelocity.Y; // keeping your Y velocity same since you have jumping.
            playerVelocity = velocity;

EDIT: in your case it should be something like this. _maxSpeed is the speed limit.

private void FixedUpdate()
    {
        Movements();
        var clampedVelocity = Vector3.ClampMagnitude(rb.velocity, _maxSpeed);
        clampedVelocity.y = rb.velocity.y;
        rb.velocity = clampedVelocity;
    }
over 4 years ago · Santiago Trujillo Denunciar

0

Try normalizing the vector. (more info is on unity docs, but normalize() will make sure that the sum of all the vector's values isnt above 1.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda