This is my RunningSound code, which uses triggers to play ‘walking’ and ‘running’ sounds. The problem is, I have no idea how to fix the CharacterMotor error I keep getting at runtime. The CharacterMotor and this file are in the same folder in the Standard Assets file, and the file below is a C# file, while the CharacterMotor is a Javascript file. Any suggestions on what I should do? Thank you in advance for your help.
using UnityEngine;
using System.Collections;
using CharacterMotor;//Unsure of this area, does not seem to do anything.
//Still here to show it does not work.
[RequireComponent(typeof(AudioSource))]
public class WalkingSoundScript : MonoBehaviour {
AudioClip walk;
AudioClip run;
float walkAudioSpeed = 0.4f;
float runAudioSpeed = 0.2f;
private float walkAudioTimer = 0.0f;
private float runAudioTimer = 0.0f;
bool isWalking = false;
bool isRunning = false;
float walkSpeed = 7; // regular speed
float runSpeed = 20; // run speed
private CharacterController chCtrl;
private CharacterMotor chMotor;//Note that CharacterMotor is a Javascript file, and is not considered an object.
void Start (){
chCtrl = GetComponent<CharacterController>();
chMotor = GetComponent<CharacterMotor>();
}
void Update (){
SetSpeed();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = 0.0f;
runAudioTimer = 0.0f;
}
}
void SetSpeed (){
float speed= walkSpeed;
if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
{
speed = runSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
}
void PlayFootsteps (){
if ((Input.GetAxis( "Horizontal" )>0) || (Input.GetAxis( "Vertical" )>0) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
// Play Audio
if ( isWalking )
{
if ( audio.clip != walk )
{
audio.Stop();
audio.clip = walk;
}
//if ( !audio.isPlaying )
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.Play();
walkAudioTimer = 0.0f;
}
}
else if ( isRunning )
{
if ( audio.clip != run )
{
audio.Stop();
audio.clip = run;
}
//if ( !audio.isPlaying )
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.Play();
runAudioTimer = 0.0f;
}
}
else
{
audio.Stop();
}
// increment timers
walkAudioTimer += Time.deltaTime;
runAudioTimer += Time.deltaTime;
}
}