Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

c# - 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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...