代码错误,预期为CS1513(绝对入门)

时间:2019-04-17 19:32:28

标签: c#

我在纠正代码最后一行的错误时遇到麻烦,其中'}'出现错误(预期为'})。我是新手,但是搜寻了我的代码却找不到问题。

我尝试删除; },将}放在距行的开头越来越近的位置。

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

}

2 个答案:

答案 0 :(得分:1)

您在函数'EngineAudio'的底部缺少结束符}因为可能使用的是Visual Studio,这是查找丢失的花括号的提示:将光标放在打开或关闭花括号上,按住控制键然后按[]键: CTRL + ] 。这会将光标移动到匹配的打开或关闭大括号。如果光标没有移动,则说明该块内部存在问题。您可以通过这种方式测试代码部分中的所有花括号,并查看丢失的花括号应该在哪里。也适用于圆括号(也称为圆括号)。检查一下:Where's the closing brace?

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

答案 1 :(得分:0)

我看到您的问题所在的代码块之外有一个},假设它进入了private void EngineAudio(),看起来您的变量开头有一个括号,而变量之后没有括号我刚刚提到。

可能是:

{ 
  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);
      }
  } // void EngineAudio()
} // missing closing bracket ?