WALKING AND SHOOTING

Our avatar (created by Unity Technologies) has 47 animations:

But we will only use those basics, which, in the case, have the names: "Idle", "WalkForward" and "ShootStraight".

We will combine, in this lesson, the administration of the animations with the act of shoot and kill/died.

To instancialize a bullet we will dismiss the gun and place the instantialization referred to the avatar.

First, let us add , in the Update function of the script "AvatarControl.js", new lines of code to trigger some RPCs:

IMPORTANT: We will shoot pressing the "t" key.

//Control  of the animations "WalkForward" e "Idle" using RPC
if(Input.GetAxis("Vertical")!= 0){
  networkView.RPC("Walk",RPCMode.All,Player.name);
}
else{  
 networkView.RPC("Idle", RPCMode.All, Player.name);
}
//Control  of the  animation "ShootStraight" using  RPC
//When shooting, the avatar stops
if(Input.GetKey("t")){
  velocity=0;
  networkView.RPC("Shoot", RPCMode.All, Player.name);
}
//Control  of the shoot using  RPC
if(Input.GetKeyDown("t")){
 var entID = Player.networkView.viewID.ToString().Remove(0,13);
 networkView.RPC("ShootB", RPCMode.All,entID,0); //the last parameter is the value of  "OneTime" in the RPC 
}
//After the shoot, the avatar walks again
if(Input.GetKeyUp("t"))  velocity = 5.0;

}

These RPCs must be placed in the script "ServCli.js" already seen, but we reproduce it in full (news in blue):

var IPServer = "127.0.0.1";
var  IPServ = "";
var bullet : Rigidbody;
var OneTime=0;
 
function OnGUI (){
  if (Network.peerType == NetworkPeerType.Disconnected){
	IPServer = GUI.TextField(new Rect(120,10,100,20),IPServer);
	if (GUI.Button (new Rect(10,10,100,30),"Conect")){
	 Network.Connect(IPServer, 25000);
	}
	if (GUI.Button (new Rect(10,50,100,30),"Start Server")){
	  Network.InitializeServer(50, 25000,false);
	  
	 for (var go : GameObject in FindObjectsOfType(GameObject)){
	  go.SendMessage("OnLoaded", SendMessageOptions.DontRequireReceiver);	
	 }
	}
 }
 
 else{  
  if(Network.isServer){
	IPServ = Network.player.ipAddress;
	GUI.Label(new Rect(140,20,250,40),"IP to be used: "+IPServ );
  }
    
  if (GUI.Button (new Rect(10,10,100,30),"Exit")){
   if(Network.isServer){
    networkView.RPC("ExitCL", RPCMode.Others);
   }
   Network.Disconnect();
   Application.Quit();
  }
 }
 
}
 
function OnConnectedToServer() {
   for (var go : GameObject in FindObjectsOfType(GameObject))
	 go.SendMessage("OnLoaded", SendMessageOptions.DontRequireReceiver);		
}
 

@RPC
function ExitCL(){
  Application.Quit();
}

@RPC
function Walk(playername : String){
  var Player = GameObject.Find(playername );
  Player.animation.CrossFade("WalkForward");
  Player.animation.wrapMode = WrapMode.Loop;
}
  
@RPC
function Shoot(playername : String){
 var Player = GameObject.Find(playername );
 Player.animation.CrossFade("ShootStraight");
 Player.animation.wrapMode = WrapMode.Once;
}
   
@RPC 
function ShootB(ent : String, OneTime:System.Int32){
 var Player = GameObject.Find("Human(Clone)"+ent); 
 if(OneTime==0){ 
  var av = GameObject.Find("Human(Clone)" + ent );
  var bulClone : Rigidbody = Instantiate(bullet,Vector3(av.transform.position.x,av.transform.position.y+1.4,av.transform.position.z ),av.transform.rotation);
  bulClone.velocity = av.transform.forward * 7;
  //To avoid collision against the shooter itself
  bulClone.gameObject.GetComponent(Renderer). enabled = false;
  bulClone.detectCollisions = false;
  yield WaitForSeconds(0.4);
  bulClone.gameObject.GetComponent(Renderer). enabled = true;
  bulClone.detectCollisions = true;
 }
 OneTime=1;
}
   
@RPC
function Idle(playername : String){
 var Player = GameObject.Find(playername );
 Player.animation.CrossFade("Idle");
 Player.animation.wrapMode = WrapMode.Once;
}

For the variable "bullet," we have to drag a prefab of an small sphere with Rigidbody,a NetworkView and an AudioSource, which will be the bullet instantialized at any shoot.

In this prefab "bullet", we have put the sound of the shot. When the bullet is instancialized it plays.

You can download a collection of sound effects (included the used here):

RIGHTCLICKING HERE

The script for the sound is:

var somClip: AudioClip;
function Start() {
 audio.clip = somClip; 
 audio.Play(); 
}

For each avatar to receive the effects of the bullet, we will have in prefab "Human", the script:

function OnTriggerEnter(other : Collider)
{
  if(other.gameObject.name=="bullet(Clone)"){
   gameObject.transform.position = GameObject.Find("Administration").transform.position;
   gameObject.transform.rotation = GameObject.Find("Administration").transform.rotation;
    Destroy(other.gameObject);
 }
}

When someone dies, returns to the starting point, which is where we put the empty gameobject "Administration."

You can put a chat created by Unity Technoligies in your game.

Download "package" to be imported

RIGHTCLICKING HERE

From inside the folder "Chat",drag the script "Chat" to the gameobject "Administration".

Drag, from Project, "bubbleskin" to the variable "Skin" in the Inspector, selected "Administration".

Download the demo. You can play it with your friends. Suggest your friends to create their personalized avatars with new textures to be accessed over the Internet. Remember that you must shoot pressing the "t":

RIGHTCLICKING HERE

COMPLETE PROJECT: You can download the project like a package:

RIGHTCLICKING HERE

In order for your server to work, even if your server application is not "in focus", you should always, when creating a multiplayer application, select in the Top Bar: Edit | Project Settings | Player and tick "Run in Background". Many errors will occur because you'll forget it!

Another thing: set "Display Resolution Dialog" to "Disabled." If the player can change the resolution quality of the game it will cause problems with the speed of movement of objects.

Remember: shoot using the key "t"!

NOTE: In this game, we have created the possibility of exchange the player textures, creating your avatar with a lot of freedom. If you do not want to use this feature, you can create many sets of textures and a window with small pictures of them that, when clicked one of them, the equivalent textures will load .


PREVIOUS LESSON
TOC HOME PAGE