new instantiated object attach to player

hey guys,

i have a big problem. in my first multiplayer game i will create a laser missile. can i attach a new instantiated gameobject to my player? my missile has a script, how used coordinates from the player laser (RaycastHit - hit.point). i am using RPC to instantiate the missile, because its a rigidbody with start velocity.

In my shooting function i call:
[code2=csharp]Vector3 SpawnVector = new Vector3(rocketSpawn.position.x, rocketSpawn.position.y, rocketSpawn.position.z );
photonView.RPC("FireLaserMissile", PhotonTargets.All, SpawnVector, rocketSpawn.rotation);[/code2]
[code2=csharp][RPC]
void FireLaserMissile(Vector3 position, Quaternion rotation)
{
Rigidbody instantiatedProjectile = Instantiate(laserRocketProjectile, position, rotation) as Rigidbody;
instantiatedProjectile.velocity = rocketSpawn.TransformDirection(Vector3.forward * autoRocketSpeed);
if(photonView.isMine){

instantiatedProjectile.name = "other";
}
else{
instantiatedProjectile.name = "othercontroll";
}
}[/code2]

so you can see that i have tried to give the missile an name. why?

Here`s my missile script homing (last version):
[code2=csharp]// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
// Do test the code! You usually need to change a few small bits.

using UnityEngine;
using System.Collections;

public class HomingLaserMissile : Photon.MonoBehaviour {
public float speed;
public float turn;
public Transform explosionEffect;
public int startDistance= 400;

private bool follow = false;
private LaserC laserscript;
private GameObject laser;
private float currentTime;
private float endTime;
private bool isActive = false;

private Vector3 correctPos = Vector3.zero;
private Quaternion correctRot = Quaternion.identity;

void Start (){
laser = GameObject.FindGameObjectWithTag("laser");
laserscript = laser.GetComponent<LaserC>();
currentTime = Time.time;
endTime = currentTime + 1.0f;
}

void Update (){

if(Time.time >= endTime){
follow = true;
}
if(follow){
if(rigidbody.name == "controll"){
rigidbody.velocity = Vector3.zero;
transform.rotation=Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(laserscript.targetPosition()- transform.position), turn*Time.deltaTime);
transform.position+=transform.forward*speed*Time.deltaTime;
}
else if(rigidbody.name == "othercontroll"){
rigidbody.velocity = Vector3.zero;
transform.rotation = correctRot;
transform.position = correctPos;
}
}

}

void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info){
if(stream.isWriting){
//stream.SendNext(transform.position);
//stream.SendNext(transform.rotation);

stream.SendNext(transform.position);
stream.SendNext(Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(laserscript.targetPosition()- transform.position), turn*Time.deltaTime));
}
else{
correctPos = (Vector3)stream.ReceiveNext();
correctRot = (Quaternion)stream.ReceiveNext();
}
}



void OnCollisionEnter ( Collision collision ){
Destroy(gameObject);
Instantiate(explosionEffect, transform.position, transform.rotation);
}


}[/code2]

i have no more ideas...

how can i solve the problem? If player 1 starts a missile, only player 1 can controll them, but the others players can see the missile on her flight to destroy.....

Any ideas? :idea:

Comments

  • Sorry, I can't help with the code (it's just too much to try out and understand).
    But: If you use Instantiate in your RPC, then the created missile is not network controlled at all. It's just some GO everybody created.
    It usually is a good idea to keep it this way. The target of the missile is clear when you shoot, so there's no benefit in synchronizing the path or something. Better let every client simulate the missile and the shooting player's client can send another RPC when it hit and did damage. Every client should be able to "simulate" the other's missiles and remove them when they hit something (no matter who actually shot).
  • Hey,

    i think i have solved the problem :shock: . now, i do not used the RPC method. I instantiate the missile with PhotonNewtwork.Instantiate().

    The Problem was (i think), that i have destroy the missile with Destroy() and not with PhotonNetwork.Destroy(). So, the timing was not perfect and when player 1 destroyed the missile, player 2 have no message over that and then player 2 controll the missile.

    ok, but if a player shoot the missile, it isn`t smooth and liquid at the other players. what can i do? The OnPhotonSerializeView don`t work!?

    This is my homing script added to the missile:

    [code2=csharp]// Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
    // Do test the code! You usually need to change a few small bits.

    using UnityEngine;
    using System.Collections;

    public class HomingLaserMissile : Photon.MonoBehaviour {
    public float speed;
    public float turn;
    public Transform explosionEffect;
    public int startDistance= 400;

    private bool follow = false;
    private LaserC laserscript;
    private GameObject laser;
    private float currentTime;
    private float endTime;
    private bool isActive = false;

    void Start (){
    laser = GameObject.FindGameObjectWithTag("laser");
    laserscript = laser.GetComponent<LaserC>();
    currentTime = Time.time;
    endTime = currentTime + 1.0f;
    }

    void Update (){
    if(Time.time >= endTime){
    follow = true;
    }
    if(follow){
    rigidbody.velocity = Vector3.zero;
    transform.localRotation=Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(laserscript.targetPosition()- transform.position), turn*Time.deltaTime);
    transform.localPosition+=transform.forward*speed*Time.deltaTime;
    }
    }


    void OnGUI(){
    if(rigidbody.name != "controll"){
    GUILayout.Label ("Name: "+rigidbody.name+" Position: "+rigidbody.transform.position);
    }
    }

    void OnCollisionEnter ( Collision collision ){
    PhotonNetwork.Destroy(gameObject);
    Instantiate(explosionEffect, transform.position, transform.rotation);
    }


    }[/code2]

    What can i do? :?:


    EDIT:

    Okay, i have solved this problem too. but then, player 1 instantiate a missile and controll it and player 2 to controll the missile from player 1 too on his display.....grrrr
  • When you have issues with control of GameObjects with PhotonViews on them, you can check the isMine property. Careful: The Master Client has extra "rights" for scene game objects and those of left players (isMine will be true for these, too).
    When you use a RPC, you need to set your own flag "owned".
    In the Marco Polo tutorial, I disabled a script and only enabled it for the client that owned the GameObject.

    As said: When you use Instantiate for short-lived items, you generate a lot of overhead. This can limit the number of players/room . There is also a"messages/second per room" limit on the cloud. It's not enforced yet in worst case, this could mean you will have to refactor your game later on.
  • Hi,

    okay. But now i have problem with the instantiated gameobjects (rigidbody). i have created a sync script. but then, the new gameobjects spawn on 0,0,0. can you tell me why?

    [code2=csharp]using UnityEngine;
    using System.Collections;

    public class SyncRigidbody : Photon.MonoBehaviour {
    public Transform movementTransform;
    public Transform rotationTransform;

    public float updateMovementSpeed = 1f;
    public float updateRotationSpeed = 1f;

    private Vector3 _desiredPosition;
    private Quaternion _desiredRotation;

    private PhotonView _photonView;
    private bool _localPlayer;

    void Awake() {
    _localPlayer = MyPhotonView.isMine;

    if(!_localPlayer) StartCoroutine("SmoothMovement");
    }

    IEnumerator SmoothMovement() {
    while(true) {
    rigidbody.position = Vector3.Lerp (rigidbody.position, _desiredPosition, Time.deltaTime * updateMovementSpeed);
    rigidbody.rotation = Quaternion.Lerp(rigidbody.rotation, _desiredRotation, Time.deltaTime * updateRotationSpeed);

    yield return null;
    }
    }

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
    if (stream.isWriting) {
    stream.SendNext(rigidbody.position);
    stream.SendNext(rigidbody.rotation);


    } else {
    _desiredPosition = (Vector3)stream.ReceiveNext();
    _desiredRotation = (Quaternion)stream.ReceiveNext();
    }
    }

    PhotonView MyPhotonView {
    get{
    if(_photonView == null) {
    _photonView = GetComponent<PhotonView>();
    }
    return _photonView;
    }
    }
    }[/code2]

    i have seen, that other used a networkrigidbody script???
  • The position to instantiate at is defined by the Instantiate call. That's not even in this code.