Raycast crashing my Client

.phase

Member
Member
Jul 29, 2014
58
12
8
Southern California
Hey guys, I've been running tests on my 1.0.5 Fougerite server, and all my Magma functions seem to be working swimmingly, except for one.

It's the Portal Gun feature I grabbed from Sleepy's TPMaster Plugin. I didn't really need all the other Teleport functions he had in his plugin, and he had a lot of trouble with timers in Magma that eventually made him throw in the towel, so I just grabbed the relevant code to use privately on my server. Anyhow, it worked fine for me in my Magma 1.1.5 server, but it's crashing my game client when I use it on Fougerite 1.0.5. The relevant snippet of code is below.

If any of you would be kind enough to try it out on your server to see if you're getting a similar problem, please let me know what you find. For me, console doesn't throw errors, but my game client stalls indefinitely, and I have to force quit and restart the game. When I join back in, the server is still running properly.

JavaScript:
        case "zip":
            if(Player.Admin || mod == "1" || trial == "1"){
                var location;
                var distArray = [];
                var hits = UnityEngine.Physics.RaycastAll(Player.PlayerClient.controllable.character.eyesRay);
                if(hits.Length > 0){
                    for(var i=0; i < hits.Length; i++){
                        distArray.push(Math.ceil(hits[i].distance));
                    }
                    var mindist = Math.min.apply(Math, distArray);
                    var targetloc = Util.Infront(Player, mindist);
                    var terrainhg = World.GetGround(targetloc.x, targetloc.z);
                    Player.TeleportTo(targetloc.x, terrainhg + 3, targetloc.z);
                }
            }
        break;
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
Hey guys, I've been running tests on my 1.0.5 Fougerite server, and all my Magma functions seem to be working swimmingly, except for one.

It's the Portal Gun feature I grabbed from Sleepy's TPMaster Plugin. I didn't really need all the other Teleport functions he had in his plugin, and he had a lot of trouble with timers in Magma that eventually made him throw in the towel, so I just grabbed the relevant code to use privately on my server. Anyhow, it worked fine for me in my Magma 1.1.5 server, but it's crashing my game client when I use it on Fougerite 1.0.5. The relevant snippet of code is below.

If any of you would be kind enough to try it out on your server to see if you're getting a similar problem, please let me know what you find. For me, console doesn't throw errors, but my game client stalls indefinitely, and I have to force quit and restart the game. When I join back in, the server is still running properly.

JavaScript:
        case "zip":
            if(Player.Admin || mod == "1" || trial == "1"){
                var location;
                var distArray = [];
                var hits = UnityEngine.Physics.RaycastAll(Player.PlayerClient.controllable.character.eyesRay);
                if(hits.Length > 0){
                    for(var i=0; i < hits.Length; i++){
                        distArray.push(Math.ceil(hits[i].distance));
                    }
                    var mindist = Math.min.apply(Math, distArray);
                    var targetloc = Util.Infront(Player, mindist);
                    var terrainhg = World.GetGround(targetloc.x, targetloc.z);
                    Player.TeleportTo(targetloc.x, terrainhg + 3, targetloc.z);
                }
            }
        break;
Past all of your code into: This it will tell you what could be wrong and why.
 

.phase

Member
Member
Jul 29, 2014
58
12
8
Southern California
The code itself is fine, and has been working perfectly in Magma 1.1.5, for some reason, though, the same function crashes my client side game app. The server is still running just fine and I can force quit my game and log back in with no problem. Console doesn't throw any errors.

A little baffled.
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
The code itself is fine, and has been working perfectly in Magma 1.1.5, for some reason, though, the same function crashes my client side game app. The server is still running just fine and I can force quit my game and log back in with no problem. Console doesn't throw any errors.

A little baffled.
Have you tried running it on Fougerite V1.0.3?
1.0.5 likes to give errors
 

Jakkee

Retired Staff
Retired Staff
Plugin Developer
Jul 28, 2014
1,465
932
113
Australia
When I had 1.0.3F loaded momentarily, I did check a few of my Magma plugin functions, and this one had the same problem.
Ah I see, Maybe download TpAdmin and compare code you might some something the same or similar. I know that TpAdmin crashes when you type /tpadmin [Name].
Do you know its the raycast crashing your client?
 

OzNumpty

New Member
Member
Sep 1, 2014
1
1
1
I found that you need to access the variables in the eyesRay first to get it to work. I don't know why. I only discovered it when I put in debug statements and then everything worked. So to get it to work I used the code below. I also found that World.GetGround doesn't work, so if you pass that to the teleport it will crash the client. You can use the point you get back from the hits value for the teleport instead. The code below is to get the "portal gun" working

JavaScript:
// Seem to need to reference variable directly or it isn't set.
var dummy1 = Player.PlayerClient.controllable.character.eyesRay.direction.magnitude;
var dummy2 = Player.PlayerClient.controllable.character.eyesRay.direction.normalized;
var dummy3 = Player.PlayerClient.controllable.character.eyesRay.direction.sqrMagnitude;
var dummy4 = Player.PlayerClient.controllable.character.eyesRay.origin.magnitude;
var dummy5 = Player.PlayerClient.controllable.character.eyesRay.origin.normalized;
var dummy6 = Player.PlayerClient.controllable.character.eyesRay.origin.sqrMagnitude;
        
var hits = UnityEngine.Physics.RaycastAll(Player.PlayerClient.controllable.character.eyesRay);    
if(hits.Length > 0){
        var closest = hits[0].point;
        var closestDistance = hits[0].distance;          
        for(var j=0; j < hits.Length ; j++ ){
             if (hits[j].distance < closestDistance) {
               closest = hits[j].point;
               closestDistance = hits[j].distance;
        }
}          
         
Player.TeleportTo(closest.x, closest.y + 3, closest.z);
}
If anybody can get World.GetGround() working, please put up the code
 
Last edited:
  • Useful
Reactions: .phase

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
I found that you need to access the variables in the eyesRay first to get it to work. I don't know why. I only discovered it when I put in debug statements and then everything worked. So to get it to work I used the code below. I also found that World.GetGround doesn't work, so if you pass that to the teleport it will crash the client. You can use the point you get back from the hits value for the teleport instead. The code below is to get the "portal gun" working

JavaScript:
// Seem to need to reference variable directly or it isn't set.
var dummy1 = Player.PlayerClient.controllable.character.eyesRay.direction.magnitude;
var dummy2 = Player.PlayerClient.controllable.character.eyesRay.direction.normalized;
var dummy3 = Player.PlayerClient.controllable.character.eyesRay.direction.sqrMagnitude;
var dummy4 = Player.PlayerClient.controllable.character.eyesRay.origin.magnitude;
var dummy5 = Player.PlayerClient.controllable.character.eyesRay.origin.normalized;
var dummy6 = Player.PlayerClient.controllable.character.eyesRay.origin.sqrMagnitude;
       
var hits = UnityEngine.Physics.RaycastAll(Player.PlayerClient.controllable.character.eyesRay);   
if(hits.Length > 0){
        var closest = hits[0].point;
        var closestDistance = hits[0].distance;         
        for(var j=0; j < hits.Length ; j++ ){
             if (hits[j].distance < closestDistance) {
               closest = hits[j].point;
               closestDistance = hits[j].distance;
        }
}         
        
Player.TeleportTo(closest.x, closest.y + 3, closest.z);
}
If anybody can get World.GetGround() working, please put up the code
looks like GetGround() is broken...
 
  • Informative
Reactions: .phase

balu92

Retired Staff
Retired Staff
Trusted Member
Jul 11, 2014
338
75
28
34
also, why RaycastAll, why not just Raycast?
 

.phase

Member
Member
Jul 29, 2014
58
12
8
Southern California
Thanks for sharing, OzNumpty. My server's been really dead lately (looks like classic Rust population has gone done a lot), so I haven't had much motivation to look into it at all. Really appreciate you sharing what you found. Hope this helps others, too. The "portal gun" is a really great tool for admins to catch hackers. Props to Sleepy also, if he's still lurking around. Hope he comes back.
 

.phase

Member
Member
Jul 29, 2014
58
12
8
Southern California
also, why RaycastAll, why not just Raycast?
Honestly, I'm not sure. The original work was done by Sleepy, I only made small adaptations to work it into a single plugin for my server. Looking at the Unity documentation, I am thinking that Raycast would not return true if the user were just pointing at some area of ground in the distance, as there are no colliders?

I'd have to test that assumption and see what the real difference is when applied on Rust Island, though. I can let everyone know tonight when I run some tests.