Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Modify Transform Of Network.Instantiate Object

Discussion in 'Multiplayer' started by mille562, Oct 16, 2008.

  1. mille562

    mille562

    Joined:
    Sep 2, 2008
    Posts:
    11
    How can I access the transform of an object created via Network.Instantiate()?

    Situation:
    I have a cannons that fires cannonballs. When the player fires a cannon, the cannon ball needs to be created on each network game and fired at the angle/position of the cannon.

    I would like to do something like:
    var cannonBall = Network.Instantiate(cball, startPos, startRotation, 0);
    cannonBall.rigidbody.velocity = Vector3(5,4,1);

    but I do not know how to access the rigidbody from the object returned from the Network.Instantiate call.
     
  2. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    There is no difference. The code you have should work fine as long as it is executed on every machine. You can do this by putting it in the OnNetworkInstantiate() function in the script attached to the cannon ball.
     
  3. mille562

    mille562

    Joined:
    Sep 2, 2008
    Posts:
    11
    I am able to edit rigidbody from OnNetworkInstantiate but then I do not know which cannon fired the ball. I need that information to determine which angle to fire the ball from.

    Note:
    I could do it by calculating the closest cannon, but what about the possibility that the cannons are exactly the same distance from the ball, which is possible in my scenario.
     
  4. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    Well ok, an alternative is to use an RPC instead of the Network.Instantiate... ends up doing the same thing. Instead of using OnNetworkInstantiate, use an RPC that is attached to the cannon, then have the cannon instantiate the ball when it receives the "Fire" RPC and set the appropriate transform velocity parameters.
     
  5. jeffcraighead

    jeffcraighead

    Joined:
    Nov 15, 2006
    Posts:
    740
    I thought of another option too. You could have the cannon ball be a prefab with a script on it that adds a velocity in the Z (or X or Y) direction as needed, then when you instantiate the prefab it will automatically be going in the correct direction. That assumes you get the transform and rotation on from the cannon on the machine that calls Network.Instantiate(); But that should work, I think I've used something similar in prototype games I've tinkered with.