Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对角线运动比正常运动快_C#_Unity3d_Controller_Rigid Bodies - Fatal编程技术网

C# 对角线运动比正常运动快

C# 对角线运动比正常运动快,c#,unity3d,controller,rigid-bodies,C#,Unity3d,Controller,Rigid Bodies,第一:对不起我的英语 大家好,我对Unity非常陌生(5天)。 今天,我为基本动作制作了一个硬体的脚本,但对角线动作比正常动作快。。我在网上搜索,但找不到我能理解的帖子 这是我的剧本。 另外,如果你知道当我们跳的时候,或者当我们跳到一个方向时,如何不移动,它会沿着这个方向,告诉我。(我知道我的英语很糟糕。)另外,我对这个网站还是新手 using System.Collections; using System.Collections.Generic; using UnityEngine; pu

第一:对不起我的英语

大家好,我对Unity非常陌生(5天)。 今天,我为基本动作制作了一个硬体的脚本,但对角线动作比正常动作快。。我在网上搜索,但找不到我能理解的帖子

这是我的剧本。 另外,如果你知道当我们跳的时候,或者当我们跳到一个方向时,如何不移动,它会沿着这个方向,告诉我。(我知道我的英语很糟糕。)另外,我对这个网站还是新手

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);
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类玩家运动:单一行为
{
[Serialized Field]专用浮动行走速度;
[SerializeField]专用浮点数运行速度;
[Field]私人浮动跳线部队;
[SerializeField]专用浮点数距离;
私人刚体;
浮动速度;
矢量3运动;
/////////////////////////////////////////////////////
void Start()
{
rb=GetComponent();
}
/////////////////////////////////////////////////////
无效更新()
{
跳跃();
}
/////////////////////////////////////////////////////
私有void FixedUpdate()
{
运动();
}
/////////////////////////////////////////////////////
私人行动()
{
float hAxis=Input.GetAxisRaw(“水平”);
float vAxis=Input.GetAxisRaw(“垂直”);
if(Input.GetButton(“Run”))
{
Vector3移动=新Vector3(hAxis,0,vAxis)*运行速度*时间.deltaTime;
Vector3 newPosition=rb.position+rb.transform.TransformDirection(移动);
rb.移动位置(新位置);
}
其他的
{
Vector3移动=新Vector3(hAxis,0,vAxis)*行走速度*时间.deltaTime;
Vector3 newPosition=rb.position+rb.transform.TransformDirection(移动);
rb.移动位置(新位置);
}
}
/////////////////////////////////////////////////////
私人跳台()
{
if(Input.GetButtonDown(“跳转”))
{
如果(isground())
{
rb.附加力(0,跳跃力,0,力模式脉冲);
}
}
}
/////////////////////////////////////////////////////
二等兵
{
返回Physics.Raycast(transform.position,Vector3.down,jumpraycast-distance);
}
}

为了保持速度不变,你需要限制速度。查看向量3.ClampMagnitude()

编辑:在你的情况下,应该是这样的。
\u maxSpeed
是速度限制

private void FixedUpdate()
    {
        Movements();
        var clampedVelocity = Vector3.ClampMagnitude(rb.velocity, _maxSpeed);
        clampedVelocity.y = rb.velocity.y;
        rb.velocity = clampedVelocity;
    }

尝试规范化向量。(更多信息在unity文档中,但是normalize()将确保所有向量值的总和不超过1。

我对单位不太熟悉,但当我读到你的问题时,我确实想知道,你的对角线移动速度比直线快约40%有问题吗?是因为水平移动10个单位的距离是10个单位,但水平移动10个单位,垂直移动10个单位的距离是10个单位吗单个时间段是
sqrt(10^2+10^2)的移动
即三角形的斜边,即在同一时间段内的移动距离为14?@CaiusJard是的,我的对角线移动得很快。你能说得更具体些吗?我试过你让我做的事情,但没有任何效果。更新了答案。对于答案,我不知道为什么,但它不起作用:/有趣,它应该起作用。可能是ab我们的执行是错误的,但不能从这里做更多。
private void FixedUpdate()
    {
        Movements();
        var clampedVelocity = Vector3.ClampMagnitude(rb.velocity, _maxSpeed);
        clampedVelocity.y = rb.velocity.y;
        rb.velocity = clampedVelocity;
    }