Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Standard Asset FP Controller not changing speed

Discussion in 'Scripting' started by ActionGabrijel, Oct 23, 2018.

  1. ActionGabrijel

    ActionGabrijel

    Joined:
    Oct 23, 2018
    Posts:
    1
    Hello!
    I would like the Controller to change speed depending on what button I push from another script.
    This does not work apparently and I can't for the life of me figure out why
    Code (CSharp):
    1. {
    2.     [RequireComponent(typeof (CharacterController))]
    3.     [RequireComponent(typeof (AudioSource))]
    4.     public class FirstPersonController : MonoBehaviour
    5.     {
    6.         [SerializeField] private bool m_IsWalking;
    7.         public float speed = 0f;
    8.         [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    9.         [SerializeField] private float m_JumpSpeed;
    10.         [SerializeField] private float m_StickToGroundForce;
    11.         [SerializeField] private float m_GravityMultiplier;
    12.         [SerializeField] private MouseLook m_MouseLook;
    13.         [SerializeField] private bool m_UseFovKick;
    14.         [SerializeField] private FOVKick m_FovKick = new FOVKick();
    15.         [SerializeField] private bool m_UseHeadBob;
    16.         [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    17.         [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    18.         [SerializeField] private float m_StepInterval;
    19.         [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    20.         [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    21.         [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    22.  
    23.         private Camera m_Camera;
    24.         private bool m_Jump;
    25.         private float m_YRotation;
    26.         private Vector2 m_Input;
    27.         private Vector3 m_MoveDir = Vector3.zero;
    28.         private CharacterController m_CharacterController;
    29.         private CollisionFlags m_CollisionFlags;
    30.         private bool m_PreviouslyGrounded;
    31.         private Vector3 m_OriginalCameraPosition;
    32.         private float m_StepCycle;
    33.         private float m_NextStep;
    34.         private bool m_Jumping;
    35.         private AudioSource m_AudioSource;
    36.  
    37.         // Use this for initialization
    38.         private void Start()
    39.         {
    40.             m_CharacterController = GetComponent<CharacterController>();
    41.             m_Camera = Camera.main;
    42.             m_OriginalCameraPosition = m_Camera.transform.localPosition;
    43.             m_FovKick.Setup(m_Camera);
    44.             m_HeadBob.Setup(m_Camera, m_StepInterval);
    45.             m_StepCycle = 0f;
    46.             m_NextStep = m_StepCycle/2f;
    47.             m_Jumping = false;
    48.             m_AudioSource = GetComponent<AudioSource>();
    49.             m_MouseLook.Init(transform , m_Camera.transform);
    50.         }
    51.  
    52.  
    53.         // Update is called once per frame
    54.         private void Update()
    55.         {
    56.             RotateView();
    57.             // the jump state needs to read here to make sure it is not missed
    58.             if (!m_Jump)
    59.             {
    60.                 m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    61.             }
    62.  
    63.             if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    64.             {
    65.                 StartCoroutine(m_JumpBob.DoBobCycle());
    66.                 PlayLandingSound();
    67.                 m_MoveDir.y = 0f;
    68.                 m_Jumping = false;
    69.             }
    70.             if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    71.             {
    72.                 m_MoveDir.y = 0f;
    73.             }
    74.  
    75.             m_PreviouslyGrounded = m_CharacterController.isGrounded;
    76.         }
    77.  
    78.         private void PlayLandingSound()
    79.         {
    80.             m_AudioSource.clip = m_LandSound;
    81.             m_AudioSource.Play();
    82.             m_NextStep = m_StepCycle + .5f;
    83.         }
    84.  
    85.  
    86.         private void FixedUpdate()
    87.         {
    88.             // always move along the camera forward as it is the direction that it being aimed at
    89.             Vector3 desiredMove = transform.forward*m_Input.y + transform.right*m_Input.x;
    90.  
    91.             // get a normal for the surface that is being touched to move along it
    92.             RaycastHit hitInfo;
    93.             Physics.SphereCast(transform.position, m_CharacterController.radius, Vector3.down, out hitInfo,
    94.                                m_CharacterController.height/2f, Physics.AllLayers, QueryTriggerInteraction.Ignore);
    95.             desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
    96.  
    97.             Debug.Log(speed);
    98.             m_MoveDir.x = desiredMove.x*speed;
    99.             m_MoveDir.z = desiredMove.z*speed;
    100.  
    101.  
    102.             if (m_CharacterController.isGrounded)
    103.             {
    104.                 m_MoveDir.y = -m_StickToGroundForce;
    105.  
    106.                 if (m_Jump)
    107.                 {
    108.                     m_MoveDir.y = m_JumpSpeed;
    109.                     PlayJumpSound();
    110.                     m_Jump = false;
    111.                     m_Jumping = true;
    112.                 }
    113.             }
    114.             else
    115.             {
    116.                 m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    117.             }
    118.             m_CollisionFlags = m_CharacterController.Move(m_MoveDir*Time.fixedDeltaTime);
    119.  
    120.             ProgressStepCycle(speed);
    121.             UpdateCameraPosition(speed);
    122.  
    123.             m_MouseLook.UpdateCursorLock();
    124.         }
    125.  
    126.  
    127.         private void PlayJumpSound()
    128.         {
    129.             m_AudioSource.clip = m_JumpSound;
    130.             m_AudioSource.Play();
    131.         }
    132.  
    133.  
    134.         private void ProgressStepCycle(float speed)
    135.         {
    136.             if (m_CharacterController.velocity.sqrMagnitude > 0 && (m_Input.x != 0 || m_Input.y != 0))
    137.             {
    138.                 m_StepCycle += (m_CharacterController.velocity.magnitude + (speed*(m_IsWalking ? 1f : m_RunstepLenghten)))*
    139.                              Time.fixedDeltaTime;
    140.             }
    141.  
    142.             if (!(m_StepCycle > m_NextStep))
    143.             {
    144.                 return;
    145.             }
    146.  
    147.             m_NextStep = m_StepCycle + m_StepInterval;
    148.  
    149.             PlayFootStepAudio();
    150.         }
    151.  
    152.  
    153.         private void PlayFootStepAudio()
    154.         {
    155.             if (!m_CharacterController.isGrounded)
    156.             {
    157.                 return;
    158.             }
    159.             // pick & play a random footstep sound from the array,
    160.             // excluding sound at index 0
    161.             int n = Random.Range(1, m_FootstepSounds.Length);
    162.             m_AudioSource.clip = m_FootstepSounds[n];
    163.             m_AudioSource.PlayOneShot(m_AudioSource.clip);
    164.             // move picked sound to index 0 so it's not picked next time
    165.             m_FootstepSounds[n] = m_FootstepSounds[0];
    166.             m_FootstepSounds[0] = m_AudioSource.clip;
    167.         }
    168.  
    169.  
    170.         private void UpdateCameraPosition(float speed)
    171.         {
    172.             Vector3 newCameraPosition;
    173.             if (!m_UseHeadBob)
    174.             {
    175.                 return;
    176.             }
    177.             if (m_CharacterController.velocity.magnitude > 0 && m_CharacterController.isGrounded)
    178.             {
    179.                 m_Camera.transform.localPosition =
    180.                     m_HeadBob.DoHeadBob(m_CharacterController.velocity.magnitude +
    181.                                       (speed*(m_IsWalking ? 1f : m_RunstepLenghten)));
    182.                 newCameraPosition = m_Camera.transform.localPosition;
    183.                 newCameraPosition.y = m_Camera.transform.localPosition.y - m_JumpBob.Offset();
    184.             }
    185.             else
    186.             {
    187.                 newCameraPosition = m_Camera.transform.localPosition;
    188.                 newCameraPosition.y = m_OriginalCameraPosition.y - m_JumpBob.Offset();
    189.             }
    190.             m_Camera.transform.localPosition = newCameraPosition;
    191.         }
    192.  
    193.         private void RotateView()
    194.         {
    195.             m_MouseLook.LookRotation (transform, m_Camera.transform);
    196.         }
    197.  
    198.  
    199.         private void OnControllerColliderHit(ControllerColliderHit hit)
    200.         {
    201.             Rigidbody body = hit.collider.attachedRigidbody;
    202.             //dont move the rigidbody if the character is on top of it
    203.             if (m_CollisionFlags == CollisionFlags.Below)
    204.             {
    205.                 return;
    206.             }
    207.  
    208.             if (body == null || body.isKinematic)
    209.             {
    210.                 return;
    211.             }
    212.             body.AddForceAtPosition(m_CharacterController.velocity*0.1f, hit.point, ForceMode.Impulse);
    213.         }
    214.     }
    215. }
    I removed run speed and changed walk speed to just speed in hopes that maybe that'll fix it but now its not working at all
    The Debug.Log(speed) part is printing out 3 on the console but I cannot move
    The part of the other script that changes movement:
    Code (CSharp):
    1. public void SetStats(int maxHP,float walk,int dmg)
    2.     {
    3.         maxHealth = maxHP;
    4.         curHealth = maxHP;
    5.         damage = dmg;
    6.         Debug.Log("called" + walk);
    7.         fpsScript.speed = walk;
    8.         Cursor.lockState = CursorLockMode.Locked;
    9.     }
    The Debug.log of that outputs called5
    I'm really new to coding so it would be really helpful if you can explain to me whats wrong and why your fix would work :D
    And if you know of a Player Controller script that has just camera movement and walking without the running and stuff that would also be very helpful!
     
  2. Miggi124

    Miggi124

    Joined:
    Jul 31, 2017
    Posts:
    69
    Hello there!

    Welcome to Unity! I hope that you find it nice here. Anyway, I'm sure there is a problem with your first script (I'll look into it later), but I can teach you how to create a basic FPS controller if you would like?

    Kind Regards,

    Gian