首页 > 解决方案 > 代码错误,应为 CS1513(绝对初学者)

问题描述

我在纠正代码最后一行的错误时遇到问题,其中“}”作为错误出现(“} 预期”)。我在这方面是全新的,但已经通过我的代码寻找并找不到问题。

我试过删除;从前一个块开始,将 } 放置在离行首越来越远的地方。

{ 
public int m_PlayerNumber = 1;
public float m_Speed = 12f;
public float m_TurnSpeed = 180f;
public AudioSource m_MovementAudio;
public AudioClip m_EngineIdling;
public AudioClip m_EngineDriving;
public float m_PitchRange = 0.2f;


private string m_MovementAxisName;
private string m_TurnAxisName;
private Rigidbody m_Rigidbody;
private float m_MovementInputValue;
private float m_TurnInputValue;
private float m_OriginalPitch;


private void Awake()
{
    m_Rigidbody = GetComponent<Rigidbody>();
}


private void OnEnable()
{
    m_Rigidbody.isKinematic = false;
    m_MovementInputValue = 0f;
    m_TurnInputValue = 0f;
}


private void OnDisable()
{
    m_Rigidbody.isKinematic = true;
}


private void Start()
{
    m_MovementAxisName = "Vertical" + m_PlayerNumber;
    m_TurnAxisName = "Horizontal" + m_PlayerNumber;

    m_OriginalPitch = m_MovementAudio.pitch;
}


private void Update()
{
    // Store the player's input and make sure the audio for the engine is playing.
    m_MovementInputValue = Input.GetAxis(m_MovementAxisName);
    m_TurnInputValue = Input.GetAxis(m_TurnAxisName);

    EngineAudio();
}


private void EngineAudio()
{
    // Play the correct audio clip based on whether or not the tank is moving and what audio is currently playing.

    if (Mathf.abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
    {
        if (m_MovementAudio.clip == m_EngineDriving)
        {
            m_MovementAudio.clip = m_EngineIdling;
            m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
            m_MovementAudio.Play();
        }
    }
    else
    {
        if (Mathf.abs(m_MovementInputValue) < 0.1f && Mathf.Abs(m_TurnInputValue) < 0.1f)
        {
            if (m_MovementAudio.clip == m_EngineIdling)
            {
                m_MovementAudio.clip = m_EngineDriving;
                m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
                m_MovementAudio.Play();
            }
        }
    }


    void FixedUpdate()
    {
        // Move and turn the tank.
        Move();
        Turn();
    }


    void Move()
    {
        // Adjust the position of the tank based on the player's input.
        Vector3 movement = transform.forward * m_MovementInputValue * m_Speed * Time.deltaTime;

        m_Rigidbody.MovePosition(m_Rigidbody.position + movement);
    }


    void Turn()
    {
        // Adjust the rotation of the tank based on the player's input.
        float turn = m_TurnInputValue * m_TurnSpeed * Time.deltaTime;

        Quaternion turnrotation = Quaternion.Euler(0f, turn, 0f);

        m_Rigidbody.MoveRotation(m_Rigidbody.rotation * turnRotation);
    }

}

标签: c#

解决方案


您在函数“EngineAudio”的底部缺少一个结束 } 由于您可能使用的是 Visual Studio,因此这里有一个查找缺少的花括号的提示:将光标放在左大括号或右大括号上,按住 control 键,然后按] 键:CTRL+ ]。这会将光标移动到匹配的左大括号或右大括号。如果光标没有移动,那么您在该块内有问题。您可以通过这种方式测试代码部分中的所有大括号,并查看缺少的大括号应该在哪里。也适用于圆括号(又名括号)。看看这个:右括号在哪里?

private void EngineAudio()
{
    // Play the correct audio clip based on whether or not the tank is moving and what audio is currently playing.

    if (Math.Abs(m_MovementInputValue) < 0.1f && Math.Abs(m_TurnInputValue) < 0.1f)
    {
        if (m_MovementAudio.clip == m_EngineDriving)
        {
            m_MovementAudio.clip = m_EngineIdling;
            m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
            m_MovementAudio.Play();
        }
    }
    else
    {
        if (Math.abs(m_MovementInputValue) < 0.1f && Math.Abs(m_TurnInputValue) < 0.1f)
        {
            if (m_MovementAudio.clip == m_EngineIdling)
            {
                m_MovementAudio.clip = m_EngineDriving;
                m_MovementAudio.pitch = Random.Range(m_OriginalPitch - m_PitchRange, m_OriginalPitch + m_PitchRange);
                m_MovementAudio.Play();
            }
        }
    }
} //<---add this one right here.

void FixedUpdate()
{
     // Move and turn the tank.
     Move();
     Turn();
}

推荐阅读