seeking help regarding a problem I'm facing while creating a footsteps script for Unity. I pasted this code (in C#) off some manual/guide on how to setup footstep sounds but when I save it and go back to Unity an error occurs, saying something like this:
Assets\Footsteps.cs(5,20): error CS0116: A namespace cannot directly contain members such as fields or methods
- I'm using Unity 2019.4.17f1.
- I'm using Microsoft Visual Studio Code 2019.
- The code was from https://coding.degree/unity-audio-tutorial/#Footstep_Sounds
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public AudioClip[] footstepClips;
public AudioSource audioSource;
public float footstepThreshold;
public float footstepRate;
private float lastFootstepTime;
public CharacterController controller;
void Update ()
{
if(controller.velocity.magnitude > footstepThreshold)
{
if(Time.time - lastFootstepTime > footstepRate)
{
lastFootstepTime = Time.time;
audioSource.PlayOneShot(footstepClips[Random.Range(0, footstepClips.Length)]);
}
}
}
Additionally, I think the code that I obtained from the coding degree website was outdated. Also if there were any grammatical mistakes in this thread that's because I made this during night time, sorry if you concluded that as a mistake.