Unty3d的Random.Range问​​题 - 我不明白它为什么会这样做

时间:2016-10-22 10:47:07

标签: c# unity3d random

所以,我使用Unity的Random.Range生成一个Vector2,这基本上就是移动应该移动的方向的x和y值。

    m_CurrentMovementInput = new Vector2
        (Random.Range(-0.75f, 0.75f), Random.Range(-0.75f, 0.75f));

问题是手机只能以对角线方向移动。这似乎就像它产生随机它产生的那样......

( - 0.35f,-0.35f)或(0.35f,-0.35f)或(-0.35f,0.35f)

当我理解随机函数时,这对我来说没什么意义。

我确信我只能一次更换一个所需的行为,但我仍然想了解它目前的行为。

编辑:我相信这是所有相关的代码。对不起,花了这么长时间不得不从回购中获取它。

void ProcessMovement()
{
    ModifyComputerMovement(m_CurrentMovementInput.x, m_CurrentMovementInput.x);
}

float ModifyMovementInput(float input)
{
    return input * m_MovementSpeedBase;
}

void RandomizeMovementInput()
{
    m_CurrentMovementInput = new Vector2
        (Random.Range(-0.75f, 0.75f), Random.Range(-0.75f, 0.75f));

    if(RandomBoolean)
    {
        m_CurrentMovementInput += new Vector2
            (Random.Range(-0.05f, 0.05f), Random.Range(-0.05f, 0.05f));
    }
}

void ModifyComputerMovement(float x, float y)
{
    m_VerticalInput = x;
    m_HorizontalInput = y;

    if ((m_VerticalInput == 0 && m_HorizontalInput == 0))
    {
        m_IsMoving = false;
    }

    else { m_IsMoving = true; }

    UpdateCpuLocation();
}

bool m_ShouldRun;
bool m_ShouldSprint;
void UpdateCpuLocation()
{
    if (!IsMoving())
        m_CurrentMoveType = MovementType.None;
    else
        m_CurrentMoveType = MovementType.Walk;

    m_DeltaVector = new Vector3
    (
        ModifyMovementInput(m_VerticalInput),
        m_IsJumping ? ComputeJumpVelocity() : ApplyGravity(),
        ModifyMovementInput(m_HorizontalInput)
    );

    m_GammaVector =
        transform.forward * m_DeltaVector.x + transform.right * m_DeltaVector.z;

    m_DeltaVector = new Vector3
        (m_GammaVector.x, m_DeltaVector.y, m_GammaVector.z);

    if (m_ShouldRun && IsMoving())
    {
        if (m_Controller.isGrounded)
        {
            m_CurrentMoveType = MovementType.Run;
            m_DeltaVector *= m_RunSprintFactor;

            if (m_ShouldSprint && IsMoving())
            {
                m_CurrentMoveType = MovementType.Sprint;
                m_DeltaVector *= m_RunSprintFactor;
            }
        }
    }

    m_VectorShift = !m_Controller.isGrounded ?
            (m_DeltaVector * Time.deltaTime) * m_JumpModifier : (m_DeltaVector * Time.deltaTime);

    if (m_Controller.isGrounded)
        m_VectorShift *= (m_PlayerState.GetCurrentEnergyLevelRatio());

    m_VectorShift = new Vector3
        (
            m_VectorShift.x,
            m_VectorShift.y,
            m_VectorShift.z
        );

    #region Debug
    //Debug.Log("Vector Shift: " + m_VectorShift);
    //Debug.Log("Vector Shift Breakdown:");
    /*Debug.Log
        (String.Format
            ("VectorShift (X): {0} | VectorShift (Y): {1} | VectorShift (Z): {2}", 
                (double)m_VectorShift.x, (double)m_VectorShift.y, (double)m_VectorShift.z));*/
    #endregion

    if (m_PlayerState.GetCurrentEnergyLevelRatio() < m_MinimumEnergyThreshold)
        m_VectorShift *= m_LowStamMoveModulus;

    //m_MoveEventArg =
        //new PlayerMoveActionEventArgs(m_VectorShift, GetCurrentMovementType);
    if (m_IsMoving)
    {
        //EventDispatcher.InvokeMoveAction(m_MoveEventArg);
    }

    m_VectorShift = m_VectorShift * (Time.deltaTime * m_TimeWarpModifier);
    m_Controller.Move(m_VectorShift);
}
  

编辑,第二部分

所以,唯一真正不同的是:

void QueryPlayerInput()
{
    m_VerticalInput = Input.GetAxis("Vertical");
    m_HorizontalInput = Input.GetAxis("Horizontal");

    if ((m_VerticalInput == 0 && m_HorizontalInput == 0))
    {
        m_IsMoving = false;
    }

    else {  m_IsMoving = true; }

    UpdatePlayerLocation();
}

0 个答案:

没有答案