Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour
- {
- public AudioSource audioS;
- CharacterController charControl;
- public float walkSpeed = 4.0f;
- void Start()
- {
- charControl = GetComponent<CharacterController>();
- }
- void Update()
- {
- MovePlayer();
- PlayAS();
- }
- void MovePlayer()
- {
- float horiz = Input.GetAxis("Horizontal");
- float vert = Input.GetAxis("Vertical");
- Vector3 moveDirSide = transform.right * horiz * walkSpeed;
- Vector3 moveDirForward = transform.forward * vert * walkSpeed;
- charControl.SimpleMove(moveDirSide);
- charControl.SimpleMove(moveDirForward);
- }
- private void PlayAS()
- {
- if (charControl.isGrounded == true && charControl.velocity.magnitude> 2f && audioS.isPlaying == false)
- {
- audioS.volume = Random.Range(0.4f, 0.5f);
- audioS.pitch = Random.Range(0.8f, 0.9f);
- audioS.Play();
- }
- if(charControl.isGrounded == true && charControl.velocity.magnitude < 2f)
- {
- audioS.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement