- Home /
[Network] Damage Not receive ?
Hi I'm got a problem with damages when i shoot to other character not happen it on See this my Machine Gun Code / This file is part of the Unity networking tutorial by M2H (http://www.M2H.nl) The original author of this code is Mike Hergaarden, even though some small parts are copied from the Unity tutorials/manuals. Feel free to use this code for your own projects, drop us a line if you made something exciting! / #pragma implicit #pragma downcast
var range = 100.0; var fireRate = 0.2; var force = 10.0; var damage = 5.0; var bulletsPerClip = 40; var clips = 10; var reloadTime = 0.5; private var hitParticles : ParticleEmitter; var muzzleFlash : Renderer;
var localPlayer : boolean = false; var localPlayerName : String = "";
private var bulletsLeft : int = 0; private var nextFireTime = 0.0; private var m_LastFrameShot = -1;
private var ammoText : String = ""; var btnTexture : Texture; var timer : float = 1; var timerMax : float = 1;
var mytrans : Transform;
function Start () { mytrans=transform;
localPlayerName=PlayerPrefs.GetString("playerName");
hitParticles = GetComponentInChildren(ParticleEmitter);
// We don't want to emit particles all the time, only when we hit something.
if (hitParticles){
hitParticles.emit = false;
}
bulletsLeft = bulletsPerClip;
GetBulletsLeft();
}
function Update(){ if(!localPlayer){ return; } }
function OnGUI() {
if(localPlayer){
GUILayout.Space(20);
GUILayout.Label("Ammo: "+ammoText);
}
if (GUI.RepeatButton(Rect(280, 280, 40, 40),btnTexture)){
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
timer += 1 * Time.deltaTime;
if(timer >= timerMax) {
Fire();
}
}
}
function LateUpdate() {
// We shot this frame, enable the muzzle flash
if (m_LastFrameShot >= Time.frameCount-3) {
//muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
if(!localPlayer){
muzzleFlash.enabled = true;
}
if (audio) {
if (!audio.isPlaying)
audio.Play();
audio.loop = true;
}
} else {
// We didn't, disable the muzzle flash
muzzleFlash.enabled = false;
// Play sound
if (audio)
{
audio.loop = false;
}
}
}
function Fire () {
if (bulletsLeft == 0){
return;
}
// If there is more than one bullet between the last and this frame
// Reset the nextFireTime
if (Time.time - fireRate > nextFireTime){
nextFireTime = Time.time - Time.deltaTime;
}
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// Did we hit anything?
if (Physics.Raycast (mytrans.position, direction, hit, range)) {
FiredOneShot(true, hit.point, hit.normal );
GetComponent(uLink.NetworkView).RPC("FiredOneShot",uLink.RPCMode.Others,false, hit.point, hit.normal);
// Send a damage message to the hit object
var settingsArray = new String[2];
settingsArray[0]=damage+"";
settingsArray[1]=localPlayerName;
hit.collider.SendMessage("ApplyDamage", settingsArray, SendMessageOptions.DontRequireReceiver);
}
bulletsLeft--;
GetBulletsLeft();
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
//enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0){
Reload();
}
nextFireTime += fireRate;
}
}
@RPC function FiredOneShot (shotOwner : boolean, hitPoint : Vector3, hitNormal : Vector3) { // Apply a force to the rigidbody we hit //if (hit.rigidbody){ // hit.rigidbody.AddForceAtPosition(force * direction, hit.point); //}
// Place the particle system for spawing out of place where we hit the surface!
// And spawn a couple of particles
if (hitParticles) {
hitParticles.transform.position = hitPoint;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitNormal);
hitParticles.Emit();
}
m_LastFrameShot = Time.frameCount;
}
function Reload () { // Wait for reload time first - then add more bullets! yield WaitForSeconds(reloadTime);
// We have a clip left reload
if (clips > 0) {
clips--;
bulletsLeft = bulletsPerClip;
if(localPlayer){
ammoText=bulletsLeft+"/"+(clips*bulletsPerClip);
}
}
}
function GetBulletsLeft () { ammoText=bulletsLeft+"/"+(clips*bulletsPerClip); return bulletsLeft; }
Answer by roamcel · Sep 13, 2011 at 06:43 PM
Besides the wall of text, which is certainly discouraging, can it be that your problem is not related to the script but rather the scene?
For example, you must be sure that all your network objects have NETWORKVIEWS attached to them.
Do you have errors in the console?
I have got some message from console it's warn that
You probably need to add a Renderer to the game object "Tank5.00(Clone)". Or your script needs to check if the component is attached before using it. PlayerScript.Awake () (at Assets/PlayerScript.js:27) UnityEngine.Object:Instantiate(Object, Vector3, Quaternion) uLink.#=qqT$rPWFcolRJHXbPnI1qCZEj6emXYABQDs5V0gt16Gs=:#=qhuW$IYdOaY$$anonymous$$SagU$$anonymous$$I7HR3q5vSrwFBzYbuhy5iB0SQ8upIt$bfTDbQ3ITt8B3qbRL(String, Vector3, Quaternion) #=q3$$anonymous$$kh2YJ7VL5x4p3$$anonymous$$2TfxXGwQjawaOk_2BU$$anonymous$$A$$anonymous$$9LU454=:#=qSkSGQyD8A_EQAgP7hSmDtQ==(String, Vector3, Quaternion) #=qqva8X95ZWilPd_q99fEBh_hOy_LDPvI56G88WbYgZ78=:#=qEpqc3SEc1$$anonymous$$9lfGd4FRYO2w==(String, NetworkViewID, NetworkPlayer, Vector3, Quaternion, String, String, String, BitStream, #=q$Lrg49si3cBPuhT$wiChSQ==) #=quX6_ig6h2lbCTpoDHPI7cmso3Xxk0$$anonymous$$rOp25xB$bW2rg=:#=qFfJI2cWc9JLWCeNnHeriUw==(NetworkViewID, NetworkPlayer, String, String, String, Vector3, Quaternion, Int32, Object[]) #=quX6_ig6h2lbCTpoDHPI7cmso3Xxk0$$anonymous$$rOp25xB$bW2rg=:#=qwgiB2pDvQvdgwP67ZDH2LQ==(NetworkPlayer, String, String, String, Vector3, Quaternion, Int32, Object[]) #=quX6_ig6h2lbCTpoDHPI7cmso3Xxk0$$anonymous$$rOp25xB$bW2rg=:#=qH41LbPalhqyRhh$$anonymous$$rO63DOA==(NetworkPlayer, String, Vector3, Quaternion, Int32, Object[]) #=quX6_ig6h2lbCTpoDHPI7cmso3Xxk0$$anonymous$$rOp25xB$bW2rg=:#=qPFOHb_3H5Gk_$btrwnd5Rw==(String, Vector3, Quaternion, Int32, Object[]) #=q3$$anonymous$$kh2YJ7VL5x4p3$$anonymous$$2TfxXGwQjawaOk_2BU$$anonymous$$A$$anonymous$$9LU454=:#=qII7OLSJ9Jbe6Z4I_xDQjNQ==(Object, Vector3, Quaternion, Int32, Object[]) uLink.Network:Instantiate(Object, Vector3, Quaternion, Int32, Object[]) GameSetup:OnNetworkLoadedLevel() (at Assets/GameSetup.js:111) UnityEngine.GameObject:Send$$anonymous$$essage(String, Send$$anonymous$$essageOptions) GameSetup:Awake() (at Assets/GameSetup.js:42)