Код: Выделить всё
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class walkingSounds : MonoBehaviour
{
public GameObject footstep;
public GameObject footstep2;
public GameObject footstep3;
public GameObject running1;
public GameObject running2;
public GameObject running3;
public int rndWalkSound;
public int rndRunSound;
public bool grounded = false;
public bool currentlyWalking = false;
public bool currentlyRunning = false;
void Start()
{
footstep.SetActive(false);
footstep2.SetActive(false);
footstep3.SetActive(false);
running1.SetActive(false);
running2.SetActive(false);
running3.SetActive(false);
}
void Update()
{
void footsteps()
{
rndWalkSound = Random.Range(1, 4);
if (currentlyWalking == false && grounded == true)
{
if (rndWalkSound == 1)
{
footstep.SetActive(true);
}
else if (rndWalkSound == 2)
{
footstep2.SetActive(true);
}
else
{
footstep3.SetActive(true);
}
}
}
void running()
{
rndRunSound = Random.Range(1, 4);
if (currentlyRunning == false && grounded == true)
{
if (rndRunSound == 1)
{
running1.SetActive(true);
}
else if (rndRunSound == 2)
{
running2.SetActive(true);
}
else
{
running3.SetActive(true);
}
}
}
void StopFootsteps()
{
footstep.SetActive(false);
footstep2.SetActive(false);
footstep3.SetActive(false);
}
void StopRunningFootsteps()
{
running1.SetActive(false);
running2.SetActive(false);
running3.SetActive(false);
}
if (Input.GetKey("w") && !Input.GetKey(KeyCode.LeftShift)
|| Input.GetKey("a") && !Input.GetKey(KeyCode.LeftShift)
|| Input.GetKey("s") && !Input.GetKey(KeyCode.LeftShift)
|| Input.GetKey("d") && !Input.GetKey(KeyCode.LeftShift))
{
footsteps();
currentlyWalking = true;
}
else if (Input.GetKey("w") && Input.GetKey(KeyCode.LeftShift)
|| Input.GetKey("a") && Input.GetKey(KeyCode.LeftShift)
|| Input.GetKey("s") && Input.GetKey(KeyCode.LeftShift)
|| Input.GetKey("d") && Input.GetKey(KeyCode.LeftShift))
{
running();
currentlyRunning = true;
}
if (Input.GetKeyUp("w")
|| Input.GetKeyUp("a")
|| Input.GetKeyUp("s")
|| Input.GetKeyUp("d")
|| Input.GetKeyDown(KeyCode.LeftShift))
{
StopFootsteps();
currentlyWalking = false;
if (!Input.GetKey("w")
&& !Input.GetKey("a")
&& !Input.GetKey("s")
&& !Input.GetKey("d"))
{
StopFootsteps();
currentlyWalking = false;
}
}
if (Input.GetKeyUp(KeyCode.LeftShift))
{
StopRunningFootsteps();
currentlyRunning = false;
}
if (grounded == false)
{
StopFootsteps();
}
}
private void OnCollisionEnter(Collision collision)
{
grounded = true;
}
private void OnCollisionExit(Collision collision)
{
grounded = false;
}
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... upposed-to
Мобильная версия