Post by BreakfastBoi on Jan 9, 2019 1:08:02 GMT
script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public AudioSource _az;
public AudioClip[] audioClipArray;
public float speed = .01f;
public float jumpSpeed = 5;
public float rotationSpeed = 100;
public float lookUpSpeed = 100;
public float viewPitchRange = 90;
public float gravity = 20;
AudioSource steps;
private bool WaitSecsRunning = false;
float pitch = 0;
Vector3 moveDirection = Vector3.zero;
Camera fpsCamera;
CharacterController cOntroller;
void Start () {
fpsCamera = GetComponentInChildren<Camera>();
cOntroller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
//Yaw
if (Mathf.Abs(Input.GetAxis("Mouse X")) > 0)
{
float yaw = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
transform.Rotate(new Vector3(0, yaw, 0));
}
//Pitch
if (Mathf.Abs(Input.GetAxis("Mouse Y")) > 0)
{
pitch -= Input.GetAxis("Mouse Y") * lookUpSpeed * Time.deltaTime;
pitch = Mathf.Clamp(pitch, -viewPitchRange, viewPitchRange);
fpsCamera.transform.localRotation = Quaternion.Euler(pitch, 0, 0);
}
//Dorectional movement & jumping
if (cOntroller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
if (WaitSecsRunning)
{
StopCoroutine("WaitSecs");
WaitSecsRunning = false;
}
}
else
{
if (moveDirection != Vector3.zero)
{
//Play walking sound
if (!WaitSecsRunning)
{
StartCoroutine("WaitSecs");
WaitSecsRunning = true;
}
_az = GetComponent<AudioSource>();
}
else
{
//Stop walking sound
if (WaitSecsRunning)
{
StopCoroutine("WaitSecs");
WaitSecsRunning = false;
}
}
}
}
else
{
//Stop walking sound
if (WaitSecsRunning)
{
StopCoroutine("WaitSecs");
WaitSecsRunning = false;
}
}
moveDirection.y -= gravity * Time.deltaTime;
cOntroller.Move(moveDirection * Time.deltaTime);
}
IEnumerator WaitSecs()
{
while (true)
{
// int randomWaitVal = Random.Range(0, 2);
//Debug.Log("Audio wait " + randomWaitVal + " Seconds");
yield return new WaitForSecondsRealtime(0.5f);
_az.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];
Debug.Log("Audio Name " + _az.clip.name);
_az.PlayOneShot(_az.clip);
Debug.Log("Audio Complete");
}
}
}
//public AudioSource _as;
//public AudioClip[] audioClipArray;
// _as = GetComponent<AudioSource>();
//while (true)
//{
//int raandomWaitVal = Random.Range(0, 1);
//Debug.Log("wait " + raandomWaitVal + " Seconds");
//yield return new WaitForSeconds(raandomWaitVal);
//_as.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];
//_as.PlayOneShot(_as.clip);
// }
video:
www.youtube.com/watch?v=YBOcoqhIPZg&feature=youtu.be
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public AudioSource _az;
public AudioClip[] audioClipArray;
public float speed = .01f;
public float jumpSpeed = 5;
public float rotationSpeed = 100;
public float lookUpSpeed = 100;
public float viewPitchRange = 90;
public float gravity = 20;
AudioSource steps;
private bool WaitSecsRunning = false;
float pitch = 0;
Vector3 moveDirection = Vector3.zero;
Camera fpsCamera;
CharacterController cOntroller;
void Start () {
fpsCamera = GetComponentInChildren<Camera>();
cOntroller = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update () {
//Yaw
if (Mathf.Abs(Input.GetAxis("Mouse X")) > 0)
{
float yaw = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
transform.Rotate(new Vector3(0, yaw, 0));
}
//Pitch
if (Mathf.Abs(Input.GetAxis("Mouse Y")) > 0)
{
pitch -= Input.GetAxis("Mouse Y") * lookUpSpeed * Time.deltaTime;
pitch = Mathf.Clamp(pitch, -viewPitchRange, viewPitchRange);
fpsCamera.transform.localRotation = Quaternion.Euler(pitch, 0, 0);
}
//Dorectional movement & jumping
if (cOntroller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
if (WaitSecsRunning)
{
StopCoroutine("WaitSecs");
WaitSecsRunning = false;
}
}
else
{
if (moveDirection != Vector3.zero)
{
//Play walking sound
if (!WaitSecsRunning)
{
StartCoroutine("WaitSecs");
WaitSecsRunning = true;
}
_az = GetComponent<AudioSource>();
}
else
{
//Stop walking sound
if (WaitSecsRunning)
{
StopCoroutine("WaitSecs");
WaitSecsRunning = false;
}
}
}
}
else
{
//Stop walking sound
if (WaitSecsRunning)
{
StopCoroutine("WaitSecs");
WaitSecsRunning = false;
}
}
moveDirection.y -= gravity * Time.deltaTime;
cOntroller.Move(moveDirection * Time.deltaTime);
}
IEnumerator WaitSecs()
{
while (true)
{
// int randomWaitVal = Random.Range(0, 2);
//Debug.Log("Audio wait " + randomWaitVal + " Seconds");
yield return new WaitForSecondsRealtime(0.5f);
_az.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];
Debug.Log("Audio Name " + _az.clip.name);
_az.PlayOneShot(_az.clip);
Debug.Log("Audio Complete");
}
}
}
//public AudioSource _as;
//public AudioClip[] audioClipArray;
// _as = GetComponent<AudioSource>();
//while (true)
//{
//int raandomWaitVal = Random.Range(0, 1);
//Debug.Log("wait " + raandomWaitVal + " Seconds");
//yield return new WaitForSeconds(raandomWaitVal);
//_as.clip = audioClipArray[Random.Range(0, audioClipArray.Length)];
//_as.PlayOneShot(_as.clip);
// }
video:
www.youtube.com/watch?v=YBOcoqhIPZg&feature=youtu.be