void Apply(bool stand, bool move, float volume, float pitch, int index) { LogManager.Log("NAAnimalBehavior::Apply received " + stand + " " + move + " " + volume + " " + pitch + " " + index); //this part must be deterministic audioSourceStand.Stop(); audioSourceMove.Stop(); NARandomizeAudioSource ras = null; if (stand) { ras = audioSourceStand.GetComponent <NARandomizeAudioSource>(); } if (move) { ras = audioSourceMove.GetComponent <NARandomizeAudioSource>(); } if (ras != null) { ras.Apply(index, volume, pitch); } if (stand) { audioSourceStand.Play(); } if (move) { audioSourceMove.Play(); } }
// Update is called once per frame void Update() { if (!NA.isClient()) //server and standalone { timer += Time.deltaTime; if (timer > currentInterval) { timer -= currentInterval; currentInterval = interval + interval * (Random.value - 0.5f) * intervalVariance; //audioSourceStand.Stop(); //audioSourceMove.Stop(); float r = Random.value; NARandomizeAudioSource ras = null; if (r < moveProbability) { moving = true; PickNextGoal(); ras = audioSourceMove.GetComponent <NARandomizeAudioSource>(); if (ras != null) { ras.Randomize(); } //audioSourceMove.Play(); } else { moving = false; ras = audioSourceStand.GetComponent <NARandomizeAudioSource>(); if (ras != null) { ras.Randomize(); } //audioSourceStand.Play(); } NetworkView nv = null; nv = GetComponent <NetworkView>(); /*if (nv == null) * nv = gameObject.transform.parent.gameObject.GetComponent<NetworkView>(); */ if (nv != null) { nv.RPC("Apply", RPCMode.All, !moving, moving, ras.GetCurrentVolume(), ras.GetCurrentPitch(), ras.GetCurrentIndex()); } else { Apply(!moving, moving, ras.GetCurrentVolume(), ras.GetCurrentPitch(), ras.GetCurrentIndex()); } } Rigidbody rb = GetComponent <Rigidbody>(); if (rb) { Vector3 velprojected = Vector3.Project(rb.velocity, transform.forward); //move if (moving) { if (velprojected.magnitude < speed) { rb.AddForce(transform.forward * acceleration); } //rb.AddForce(transform.forward*10f); } else { /* * if (velprojected.magnitude > 0) * { * rb.AddForce(-transform.forward*100f); * } */ //slow down //rb.AddForce(-rb.velocity*acceleration); } } } else { //nothing, the client will have his RPC called by the server } }