Hello, I’m asking this question because I just started coding, maybe three months? And I know most basic things and I just need a little bit more guidance for networking. So I have a raycast shooting script (that has a spark on hit.raycast semi automatic) and a mouse look for 3spines in the char (x axis) so the char looks up and down moving. My question is, how can I RPC them? i just started networking and I searched but nothing showsup. Here are my codes:
Raycast
// going to add if(networkView.isMine for raycast script and mouse look)
var Spark : Transform;
var damage : float;
var fireRate : float;
var spread : float;
var recoil : float;
var weight : float;
private var nextFire = 0.0;
function Update () {
if(Input.GetButtonUp("Fire1")&&Time.time > nextFire) {
nextFire = Time.time + fireRate;
Fire ();
}
}
function Fire () {
var hit : RaycastHit;
Debug.DrawRay(transform.position, transform.forward);
if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, Mathf.Infinity)) {
if(hit.transform.gameObject.tag == "Player")
hit.transform.SendMessage("M_Damage", damage);
Spark.position = hit.point;
}
}
//mouse look
using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character:
/// - Create a capsule.
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSInputController script to the capsule
/// -> A CharacterMotor and a CharacterController component will be automatically added.
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationY = 0F;
void Update ()
{
if (axes == RotationAxes.MouseXAndY)
{
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
}
else if (axes == RotationAxes.MouseX)
{
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
}
Let me get this straight, you want to send the ‘Fire’ function over the network so that other players get hit/see the sparks on the wall and you also want to send the mouse look over the network so players can see where other players are aiming/looking?
This is going to be non-authoritative so if you want to prevent hacking you’d have to set up a dedicated server. Unity can actually be used as a dedicated server for a small number of players or there’s Photon or a number of other solutions.
The reason it’s this way is because since the messages come from the player(client)'s computer, they could send anything they want. For example where you have damage here
hit.transform.SendMessage(“M_Damage”, damage);
they could just set damage to be a billion. Boom headshot. Anyway, you’d want to replace that line of code with the RPC itself.
Here’s a rough outline on what needs to be done for a non-athoritative networking session.
Have a list of players. In that list, store important information about the player like their team if you have t vs ct or their character model if you have that, their gun type, their position, current health, score, and a unique identification number. You can just store all this info in a class then make a list of that class. Something like this:
public class PlayerInfo
{
int team;
GameObject model;
GameObject gun;
Vector3 position;
int health;
int score;
int ID;
}
public class GameMaster
{
List<PlayerInfo> player_list = new List<PlayerInfo>();
}
Give the player the ability to create a game or search for one. If they click create, register it with MasterServer.RegisterHost. If they search, call MasterServer.PollHostList and display the returned array as a list of games for them to join. Either way have a script that starts when the player enters a game and broadcast their PlayerInfo class via RPC. Use the functions OnConnectedToServer() and OnPlayerConnected() to automatically make the RPC call. The client would send out their own info and the server would send the entire current list to that client. RPCMode.AllBuffered will send the same RPC to future connections so new players only have to send out their info once.
Since RPC doesn’t support sending complex types, the PlayerInfo class can be serialized or just send the ints manually and reconstruct them on the other end. For example just have an enum with the names of each gun or model type, then have a matching array with the actual game objects, cast the enum to an int and send it over the RPC.
Now that each player has a list of all other players, update. When the local player uses movement keys, send it to RPCMode.All. This is the simple way, but can be laggy and inaccurate, a better way is to predict where the player is moving based on their input instead of snapping them to a certain location which can look choppy. To make sure the prediction is accurate, one special RPC can be sent at the beginning and end of an input to align the position for example Input.GetButtonUp().
Now that players are there and moving, replace the hit test line of code above with an RPC and trigger the fire events on each player’s computer. Recalculate health and send out if necessary.
For the mouse look, just send out an RPC containing the direction Vector3. It’s more complicated than it sounds but that should give you a start. Hope that helps!