C# 同时按下两个按钮,字符运行速度更快

C# 同时按下两个按钮,字符运行速度更快,c#,unity3d,C#,Unity3d,我正在尝试为我的角色制作控制器脚本。一切正常,但当我同时按下两个移动按钮时,我的角色移动得更快 如果我按D它的移动速度比按D+S或D+W慢。我怎样才能解决这个问题?我希望我的角色以相同的速度移动 代码: 动画角色动画; 字符控制器; 变换摄像机; 矢量3向前; 向量3;右; 矢量3运动; 水平浮动; 垂直浮动; 漂浮短跑; 浮点输入量; 浮动速度; 浮动默认速度=5; 浮速=10; void Start() { 控制器=GetComponent(); 动画=GetComponent(); cam

我正在尝试为我的角色制作控制器脚本。一切正常,但当我同时按下两个移动按钮时,我的角色移动得更快

如果我按
D
它的移动速度比按
D+S
D+W
慢。我怎样才能解决这个问题?我希望我的角色以相同的速度移动

代码:

动画角色动画;
字符控制器;
变换摄像机;
矢量3向前;
向量3;右;
矢量3运动;
水平浮动;
垂直浮动;
漂浮短跑;
浮点输入量;
浮动速度;
浮动默认速度=5;
浮速=10;
void Start()
{
控制器=GetComponent();
动画=GetComponent();
camera=camera.main.transform;
}
void FixedUpdate()
{
GetMoveInput();
}
public void GetMoveInput()
{
水平=输入.GetAxis(“水平”);
垂直=输入。GetAxis(“垂直”);
sprint=Input.GetAxis(“sprint”);
如果(水平!=0 | |垂直!=0)
{
如果(冲刺!=0)
{
速度=冲刺速度;
}
其他的
{
速度=默认速度;
}
CalculateCamera();
Move();
}
}
公共空间计算领域()
{
camForward=camera.forward;
camRight=camera.right;
camForward.y=0;
camRight.y=0;
camForward=camForward.normalized;
camRight=camRight.normalized;
}
公开作废动议()
{
运动=(凸轮前进*垂直+凸轮右*水平);
inputMagnitude=新矢量2(水平、垂直).sqrMagnitude;
控制器。移动(运动*速度*时间。延时);
动画。动画移动(输入幅度);
transform.rotation=四元数。LookRotation(运动);
}
我尝试了不同的方法,比如按下两个按钮,就可以使用
/2
。好像我做错了什么。

如果
运动具有
sqrMagnitude>1
(=
magnitude>1
,但访问成本较低),则应使用
运动

AnimationsCharacter animations;
CharacterController controller;
Transform camera;

Vector3 camForward;
Vector3 camRight;
Vector3 motion;

float horizontal;
float vertical;
float sprint;
float inputMagnitude;

float speed;
float defaultSpeed = 5;
float sprintSpeed = 10;
void Start()
{
    controller = GetComponent<CharacterController>();
    animations = GetComponent<AnimationsCharacter>();
    camera = Camera.main.transform;
}
void FixedUpdate()
{
    GetMoveInput();
}

public void GetMoveInput()
{
    horizontal = Input.GetAxis("Horizontal");
    vertical = Input.GetAxis("Vertical");
    sprint = Input.GetAxis("Sprint");

    if (horizontal != 0 || vertical != 0)
    {
        if(sprint != 0)
        {
            speed = sprintSpeed;
        }
        else
        {
            speed = defaultSpeed;
        }
        CalculateCamera();
        Move();
    }
}

public void CalculateCamera()
{
    camForward = camera.forward;
    camRight = camera.right;

    camForward.y = 0;
    camRight.y = 0;

    camForward = camForward.normalized;
    camRight = camRight.normalized;
}

public void Move()
{
    motion = (camForward * vertical + camRight * horizontal);
    inputMagnitude = new Vector2(horizontal, vertical).sqrMagnitude;
    controller.Move(motion * speed * Time.deltaTime);
    animations.AnimMovement(inputMagnitude);
    transform.rotation = Quaternion.LookRotation(motion);
}
motion = (camForward * vertical + camRight * horizontal);
if(motion.sqrMagnitude > 1)
{
    motion.Normalize();
}