Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerController : MonoBehaviour
- {
- public AudioSource audioS;
- CharacterController charControl;
- public float walkSpeed = 0.1f;
- public float gravity = 0.1f;
- public float jumpSpeed = 6f;
- private Vector3 moveDirection;
- void Start()
- {
- charControl = GetComponent<CharacterController>();
- }
- void Update()
- {
- MovePlayer();
- PlayAS();
- }
- void MovePlayer()
- {
- if (charControl.isGrounded)
- {
- moveDirection = transform.TransformDirection(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))) * walkSpeed;
- if (Input.GetButton("Jump"))
- moveDirection.y = jumpSpeed;
- }
- moveDirection.y -= gravity * Time.deltaTime;
- charControl.Move(moveDirection * Time.deltaTime);
- }
- private void PlayAS()
- {
- Debug.Log(charControl.velocity.magnitude + " = My Character Velocity");
- if (charControl.isGrounded == true && audioS.isPlaying == false && charControl.velocity.magnitude > 1f)
- {
- audioS.volume = Random.Range(0.4f, 0.5f);
- audioS.pitch = Random.Range(0.8f, 0.9f);
- audioS.Play();
- }
- if (charControl.isGrounded == false || charControl.velocity.magnitude < 2f)
- {
- audioS.Stop();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement