I know this is peeing into the wind but I thought I would start this with a question rather than posting every bit of code i have.
I have been working on a multiplayer simple game. However when I now connect to the game I am having issues where when you are controlling your character all others on the screen move…
It must be something stupid I am doing,
I am willing to send the project to anyone if they can help?
Here is the control code. Its very pieced together from many tutorials.
private var jumpSpeed:float = 8.0;
private var gravity:float = 20.0;
private var runSpeed:float = 11.0;
private var walkSpeed:float = 2.0;
private var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
function Update ()
{
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
/*if (GUIUtility.hotControl == 0) {
if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
Screen.lockCursor = true;
else
Screen.lockCursor = false;
}*/
// Toggle walking/running with the T key
if(Input.GetKeyDown("t"))
isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
@script RequireComponent(CharacterController)
Is this control script getting disabled on all remote instances of the network prefab (ie: instances in which the local player is not the owner of the NetworkView?)
Oh by the way I have my isMine in anothner script.
function OnNetworkInstantiate (msg : NetworkMessageInfo) {
// This is our own player
if (networkView.isMine)
{
//camera.main.enabled=false;
localPlayer=true;
networkView.RPC("setName", RPCMode.Others, thisName);
Destroy(GameObject.Find("LevelCamera"));
thisName=PlayerPrefs.GetString("playerName");
}
Right all I have revised the script a little turned 2 scripts into 1. Here is the full instanciating script with the controller in it.
Still doesnt work. I am able to control both characters locally for some reason
#pragma strict
#pragma implicit
#pragma downcast
var thisName : String = "Bugged name";
var rigidBodyView : NetworkView;
var hp : int = 100;
var theScoreBoard : scoreBoard;
var localPlayer : boolean = false;
var metalMaterial : Material;
private var orgMaterial : Material;
private var coloredUntill : float;
private var invincible : boolean;
private var jumpSpeed:float = 8.0;
private var gravity:float = 20.0;
private var runSpeed:float = 11.0;
private var walkSpeed:float = 2.0;
private var rotateSpeed:float = 150.0;
private var grounded:boolean = false;
private var moveDirection:Vector3 = Vector3.zero;
private var isWalking:boolean = false;
private var moveStatus:String = "idle";
function Awake(){
orgMaterial = renderer.material;
theScoreBoard= GameObject.Find("Generalscripts").GetComponent(scoreBoard);
}
function OnNetworkInstantiate (msg : NetworkMessageInfo) {
// This is our own player
if (networkView.isMine)
{
//camera.main.enabled=false;
localPlayer=true;
networkView.RPC("setName", RPCMode.Others, thisName);
Destroy(GameObject.Find("LevelCamera"));
thisName=PlayerPrefs.GetString("playerName");
}
// This is just some remote controlled player, don't execute direct
// user input on this. DO enable multiplayer controll
else
{
thisName="Remote"+Random.Range(1,10);
name += thisName;
transform.Find("CrateCamera").gameObject.active=false;
var tmp2 : FPSWalker = GetComponent(FPSWalker);
tmp2.enabled = false;
var tmp5 : MouseLook = GetComponent(MouseLook);
tmp5.enabled = false;
networkView.RPC("askName", networkView.viewID.owner, Network.player);
}
}
function Update ()
{
// Only allow control to local player
// Only allow movement and jumps while grounded
if(grounded) {
moveDirection = new Vector3((Input.GetMouseButton(1) ? Input.GetAxis("Horizontal") : 0),0,Input.GetAxis("Vertical"));
// if moving forward and to the side at the same time, compensate for distance
// TODO: may be better way to do this?
if(Input.GetMouseButton(1) && Input.GetAxis("Horizontal") && Input.GetAxis("Vertical")) {
moveDirection *= .7;
}
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= isWalking ? walkSpeed : runSpeed;
moveStatus = "idle";
if(moveDirection != Vector3.zero)
moveStatus = isWalking ? "walking" : "running";
// Jump!
if(Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
// Allow turning at anytime. Keep the character facing in the same direction as the Camera if the right mouse button is down.
if(Input.GetMouseButton(1)) {
transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0);
} else {
transform.Rotate(0,Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
}
/*if (GUIUtility.hotControl == 0) {
if(Input.GetMouseButton(1) || Input.GetMouseButton(0))
Screen.lockCursor = true;
else
Screen.lockCursor = false;
}*/
// Toggle walking/running with the T key
if(Input.GetKeyDown("z"))
isWalking = !isWalking;
//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//Move controller
var controller:CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.Below) != 0;
}
@script RequireComponent(CharacterController)
function OnGUI(){
if(localPlayer){
GUILayout.Label("HP: "+hp);
}
}
@RPC
function StartInvincibility(){
invincible=true;
renderer.material=metalMaterial;
yield new WaitForSeconds (10);
renderer.material=orgMaterial;
invincible=false;
}
function ApplyDamage (info : String[]){
var damage : float= parseFloat(info[0]);
var killerName : String= info[1];
if(invincible){
return;
}
hp -= damage;
if(hp<0){
theScoreBoard.LocalPlayerHasKilled();
networkView.RPC("Respawn",RPCMode.All);
}else{
networkView.RPC("setHP",RPCMode.Others, hp);
}
}
@RPC
function setHP(newHP : int){
hp=newHP;
}
@RPC
function Respawn(){
if (networkView.isMine)
{
theScoreBoard.LocalPlayerDied();
// Randomize starting location
var spawnpoints : GameObject[] = GameObject.FindGameObjectsWithTag ("Spawnpoint");
var spawnpoint : Transform = spawnpoints[Random.Range(0, spawnpoints.length)].transform;
transform.position=spawnpoint.position;
transform.rotation=spawnpoint.rotation;
}
hp=100;
}
@RPC
function setName(name : String){
thisName=name;
}
@RPC
function askName(asker : NetworkPlayer){
networkView.RPC("setName", asker, thisName);
}