Unity 5 FPSController walking on its own for a second

Hello i have this problem with unity unlike games i play with that are made with unity i am following Jimmy Vegas tutorial on making a game but with whatever project i make Unity’s standerd FPSController walks on its own for like 0.5-1 seconds then stops,

in any direction too, this is the script:

using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
using Random = UnityEngine.Random;

namespace UnityStandardAssets.Characters.FirstPerson
{
[RequireComponent(typeof(CharacterController))]
[RequireComponent(typeof(AudioSource))]
publicclassFirstPersonController:MonoBehaviour
{
[SerializeField]privateboolm_IsWalking;
[SerializeField]privatefloatm_WalkSpeed;
[SerializeField]privatefloatm_RunSpeed;
[SerializeField][Range(0f,1f)]privatefloatm_RunstepLenghten;
[SerializeField]privatefloatm_JumpSpeed;
[SerializeField]privatefloatm_StickToGroundForce;
[SerializeField]privatefloatm_GravityMultiplier;
[SerializeField]privateMouseLookm_MouseLook;
[SerializeField]privateboolm_UseFovKick;
[SerializeField]privateFOVKickm_FovKick=newFOVKick();
[SerializeField]privateboolm_UseHeadBob;
[SerializeField]privateCurveControlledBobm_HeadBob=newCurveControlledBob();
[SerializeField]privateLerpControlledBobm_JumpBob=newLerpControlledBob();
[SerializeField]privatefloatm_StepInterval;
[SerializeField]privateAudioClip[]m_FootstepSounds;//anarrayoffootstepsoundsthatwillberandomlyselectedfrom.
[SerializeField]privateAudioClipm_JumpSound;//thesoundplayedwhencharacterleavestheground.
[SerializeField]privateAudioClipm_LandSound;//thesoundplayedwhencharactertouchesbackonground.

privateCameram_Camera;
privateboolm_Jump;
privatefloatm_YRotation;
privateVector2m_Input;
privateVector3m_MoveDir=Vector3.zero;
privateCharacterControllerm_CharacterController;
privateCollisionFlagsm_CollisionFlags;
privateboolm_PreviouslyGrounded;
privateVector3m_OriginalCameraPosition;
privatefloatm_StepCycle;
privatefloatm_NextStep;
privateboolm_Jumping;
privateAudioSourcem_AudioSource;

//Usethisfor initialization
privatevoidStart()
{
m_CharacterController=GetComponent<CharacterController>();
m_Camera=Camera.main;
m_OriginalCameraPosition=m_Camera.transform.localPosition;
m_FovKick.Setup(m_Camera);
m_HeadBob.Setup(m_Camera,m_StepInterval);
m_StepCycle=0f;
m_NextStep=m_StepCycle/2f;
m_Jumping=false;
m_AudioSource=GetComponent<AudioSource>();
m_MouseLook.Init(transform,m_Camera.transform);
}


//Updateiscalledonceper frame
privatevoidUpdate()
{
RotateView();
//thejumpstateneedstoreadheretomakesureitisnot missed
if(!m_Jump)
{
m_Jump=CrossPlatformInputManager.GetButtonDown("Jump");
}

if(!m_PreviouslyGrounded&&m_CharacterController.isGrounded)
{
StartCoroutine(m_JumpBob.DoBobCycle());
PlayLandingSound();
m_MoveDir.y=0f;
m_Jumping=false;
}
if(!m_CharacterController.isGrounded&& !m_Jumping&&m_PreviouslyGrounded)
{
m_MoveDir.y=0f;
}

m_PreviouslyGrounded=m_CharacterController.isGrounded;
}


privatevoidPlayLandingSound()
{
m_AudioSource.clip=m_LandSound;
m_AudioSource.Play();
m_NextStep=m_StepCycle+.5f;
}


privatevoidFixedUpdate()
{
floatspeed;
GetInput(outspeed);
//alwaysmovealongthecameraforwardasitisthedirectionthatitbeingaimed at
Vector3desiredMove=transform.forward*m_Input.y+transform.right*m_Input.x;

//getanormalforthesurfacethatisbeingtouchedtomovealong it
RaycastHithitInfo;
Physics.SphereCast(transform.position,m_CharacterController.radius,Vector3.down,outhitInfo,
m_CharacterController.height/2f, ~0,QueryTriggerInteraction.Ignore);
desiredMove=Vector3.ProjectOnPlane(desiredMove,hitInfo.normal).normalized;

m_MoveDir.x=desiredMove.x*speed;
m_MoveDir.z=desiredMove.z*speed;


if(m_CharacterController.isGrounded)
{
m_MoveDir.y=-m_StickToGroundForce;

if(m_Jump)
{
m_MoveDir.y=m_JumpSpeed;
PlayJumpSound();
m_Jump=false;
m_Jumping=true;
}
}
else
{
m_MoveDir+=Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
}
m_CollisionFlags=m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);

ProgressStepCycle(speed);
UpdateCameraPosition(speed);

m_MouseLook.UpdateCursorLock();
}


privatevoidPlayJumpSound()
{
m_AudioSource.clip=m_JumpSound;
m_AudioSource.Play();
}


privatevoidProgressStepCycle(floatspeed)
{
if(m_CharacterController.velocity.sqrMagnitude>0&&(m_Input.x !=0 || m_Input.y !=0))
{
m_StepCycle+=(m_CharacterController.velocity.magnitude+(speed*(m_IsWalking?1f:m_RunstepLenghten)))*
Time.fixedDeltaTime;
}

if(!(m_StepCycle>m_NextStep))
{
return;
}

m_NextStep=m_StepCycle+m_StepInterval;

PlayFootStepAudio();
}


privatevoidPlayFootStepAudio()
{
if(!m_CharacterController.isGrounded)
{
return;
}
//pick&playarandomfootstepsoundfromthearray,
//excludingsoundatindex 0
intn=Random.Range(1,m_FootstepSounds.Length);
m_AudioSource.clip=m_FootstepSounds[n];
m_AudioSource.PlayOneShot(m_AudioSource.clip);
//movepickedsoundtoindex0soit'snotpickednext time
m_FootstepSounds[n]=m_FootstepSounds[0];
m_FootstepSounds[0]=m_AudioSource.clip;
}


privatevoidUpdateCameraPosition(floatspeed)
{
Vector3newCameraPosition;
if(!m_UseHeadBob)
{
return;
}
if(m_CharacterController.velocity.magnitude>0&&m_CharacterController.isGrounded)
{
m_Camera.transform.localPosition=
m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude+
(speed*(m_IsWalking?1f:m_RunstepLenghten)));
newCameraPosition=m_Camera.transform.localPosition;
newCameraPosition.y=m_Camera.transform.localPosition.y-m_JumpBob.Offset();
}
else
{
newCameraPosition=m_Camera.transform.localPosition;
newCameraPosition.y=m_OriginalCameraPosition.y-m_JumpBob.Offset();
}
m_Camera.transform.localPosition=newCameraPosition;
}


privatevoidGetInput(outfloatspeed)
{
//Read input
floathorizontal=CrossPlatformInputManager.GetAxis("Horizontal");
floatvertical=CrossPlatformInputManager.GetAxis("Vertical");

boolwaswalking=m_IsWalking;

#if!MOBILE_INPUT
//Onstandalonebuilds,walk/runspeedismodifiedbyakeypress.
//keeptrackofwhetherornotthecharacteriswalkingor running
m_IsWalking= !Input.GetKey(KeyCode.LeftShift);
#endif
//setthedesiredspeedtobewalkingor running
speed=m_IsWalking?m_WalkSpeed:m_RunSpeed;
m_Input=newVector2(horizontal,vertical);

//normalizeinputifitexceeds1incombinedlength:
if(m_Input.sqrMagnitude>1)
{
m_Input.Normalize();
}

//handlespeedchangetogiveanfov kick
//onlyiftheplayerisgoingtoarun,isrunningandthefovkickistobe used
if(m_IsWalking !=waswalking&&m_UseFovKick&&m_CharacterController.velocity.sqrMagnitude>0)
{
StopAllCoroutines();
StartCoroutine(!m_IsWalking?m_FovKick.FOVKickUp():m_FovKick.FOVKickDown());
}
}


privatevoidRotateView()
{
m_MouseLook.LookRotation(transform,m_Camera.transform);
}


privatevoidOnControllerColliderHit(ControllerColliderHithit)
{
Rigidbodybody=hit.collider.attachedRigidbody;
//dontmovetherigidbodyifthecharacterisontopof it
if(m_CollisionFlags==CollisionFlags.Below)
{
return;
}

if(body==null || body.isKinematic)
{
return;
}
body.AddForceAtPosition(m_CharacterController.velocity*0.1f,hit.point,ForceMode.Impulse);
}
}
}

Put your scripts into code tags (under insert/code). It helps the visibility a lot

1 Like

In addition to the above good advice, in the GetInput() method try changing

floathorizontal=CrossPlatformInputManager.GetAxis("Horizontal");
floatvertical=CrossPlatformInputManager.GetAxis("Vertical");

to

floathorizontal=CrossPlatformInputManager.GetAxisRaw("Horizontal");
floatvertical=CrossPlatformInputManager.GetAxisRaw("Vertical");

because the version in your code has a ‘speedup’ and ‘slowdown’ factor built into the input. The GetAxisRaw version returns zero or 1 only.

Thanks man you are the most helpful guy eva! now is there a way for me to make the stopping smoother perhaps

You could script a deceleration behaviour when you detect 0 input on the axis, using GetAxisRaw(), or, possibly easier to do would be to revert to using GetAxis() and then modify the Axes settings for horizontal and vertical input in the InputManager (Edit->Project Settings->Input)

I believe the sensitivity setting determines how fast it goes from zero to 1 when it detects input, and the gravity setting determines how quickly it slows back down to zero when there is no input detected. The default setting for both is 3.

So for your case, if you want it to slow down faster than normal, but not instantly, you should try changing the gravity setting to something higher than 3, until you achieve the effect you want.