- Home /
Slow time for every enemy at a certain distance from inside RPC
SO i'm creating a slow motion effect that can be used against enemies when they are at a distance of 200 or less away from "me"(player).
here is the RPC code. it work to the point where it slows the time down across the network but it is slowing time for every single player and not just those who are at a distance of 200.
this script is placed on my player.
[RPC]
void SlowTimeForEveryEnemyCloseToMe( float TScale)
{
// check which team i am on
if (TeamImOn == "Red") {
if (GameObject.FindGameObjectsWithTag ("blueTeam") != null) {
gos = GameObject.FindGameObjectsWithTag ("blueTeam");
}
}
if (TeamImOn == "Blue") {
if (GameObject.FindGameObjectsWithTag ("redTeam") != null) {
gos = GameObject.FindGameObjectsWithTag ("redTeam");
}
}
// check distance
try
{
foreach (GameObject go in gos) {
float dist;
dist = Vector3.Distance (go.transform.position, transform.position);
if (dist < 200.0f) {
Time.timeScale = TScale ;
}
}
}
catch
{
return;
}
}
From what I can tell, this is checking if any RPC is less than 200 away from you.
You may need to add in some kind of boolean value where is checks if that specific RPC is close.
Something like:
if(this.transform <= 200)
{
Time.timeScale = TScale;
}
However, I assumed time scale was for the whole game, not just one object but I may be wrong
If you're talking about a slow-mo effect like something how the wizards do in Diablo 3, then you're affecting the other specific in-range characters and not the games timeScale. While the other characters are under the effect you'll have to temporarily change their attributes/etc.
eg :
OnTrigEnter()
{
moveSpeed = slowedSpeed ;
animationSpeed = slowAnim ;
attackSpeed = slowAtk ;
}
OnTrigExit()
{
moveSpeed = normalSpeed ;
...etc etc...
}
http://docs.unity3d.com/Documentation/ScriptReference/Time-timeScale.html
thanks g0tNoodles but i think that would bring about the same effect maybe even a bit more problems.
thanks Lo0Nuhtik ,i think that would be a bit tedious for me to implement right now , besides i'm trying to slow the entire scene of the close enemies and not just the players themselves.
Answer by fholm · Mar 27, 2014 at 09:37 AM
If you are using an authoritative setup, this would be the responsibility of the server. If you are not running a central server, each client would check their own slowmo-state.
For the non-server approach, this is the basics:
1) Send an RPC to the NetworkView of the object which has the slowmo effect around itself 2) This sets a bool which says slowmo active/not active 3) On each local client, you check if they are within range of another player which has "slowmo" set to true (and its on the other team) 4) If they are slow them down, if they are not, speed them up
In my case the server can act as a player also. I wont be able to do that because "Send an RPC to the NetworkView of the object which has the slowmo effect around itself" does not exist for me .
i'm basically trying to tell all the enemies withing a 200 unity radius that "hey , you are close to me, take this RPC". so prior to the player sending the RPC there really is no slow motion function initialized across the network.
And I'm saying that this is the wrong way to go about things, synchronize the state of the object which has the Slow$$anonymous$$o effect around it ins$$anonymous$$d, and then figure out when to activate/de-activate it for each client locally ins$$anonymous$$d. It uses less bandwidth and it leads to a more consistent world since the clients don't need to be notified by RPC when they go in/out of the slow-motion field.
ooh now i understand , that "Send an RPC to the NetworkView of the object which has the slowmo effect around itself" confused me . because of how it was worded .
that should definitely work without a problem.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141233im_/https://answers.unity.com/themes/thub/images/avi.jpg)