Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public class PlayerController : MonoBehaviour
  2. {
  3. public AudioSource audioS;
  4.  
  5. CharacterController charControl;
  6. public float walkSpeed = 0.1f;
  7. public float gravity = 0.1f;
  8. public float jumpSpeed = 6f;
  9. private Vector3 moveDirection;
  10.  
  11. void Start()
  12. {
  13. charControl = GetComponent<CharacterController>();
  14. }
  15.  
  16. void Update()
  17. {
  18. MovePlayer();
  19. PlayAS();
  20. }
  21.  
  22. void MovePlayer()
  23. {
  24. if (charControl.isGrounded)
  25. {
  26. moveDirection = transform.TransformDirection(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))) * walkSpeed;
  27. if (Input.GetButton("Jump"))
  28. moveDirection.y = jumpSpeed;
  29. }
  30. moveDirection.y -= gravity * Time.deltaTime;
  31. charControl.Move(moveDirection * Time.deltaTime);
  32.  
  33. }
  34. private void PlayAS()
  35. {
  36. Debug.Log(charControl.velocity.magnitude + " = My Character Velocity");
  37. if (charControl.isGrounded == true && audioS.isPlaying == false && charControl.velocity.magnitude > 1f)
  38. {
  39. audioS.volume = Random.Range(0.4f, 0.5f);
  40. audioS.pitch = Random.Range(0.8f, 0.9f);
  41. audioS.Play();
  42. }
  43.  
  44. if (charControl.isGrounded == false || charControl.velocity.magnitude < 2f)
  45. {
  46. audioS.Stop();
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement