- Home /
UNET - How do I properly instantiate Rigidbody2D over network?
So I've started working with UNET to set up multiplayer in a project I'm playing around in and it works great EXCEPT for one thing: I don't actually know how to properly instantiate a Rigidbody2D anymore and then get it to move...
Prior to UNET I would usually do something along the lines of:
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Rigidbody2D clone = Network.Instantiate(bullet, gun.transform.position, transform.rotation, 0) as Rigidbody2D;
clone.GetComponent<Rigidbody2D>().AddForce(transform.up * bulletSpeed);
}
}
This no longer works with UNET. I've tried using NetworkServer.Spawn(bullet) and I've registered the GameObject with the NetworkManager and this sorta works; however, this causes my bullets to sit idle after firing...
Suffice to say, UNET is awesome but not being able to find documentation on how to properly instantiate a Rigidbody and whatnot is kinda frustrating...
Any help would be awesome.
Answer by Dragon_Developer · May 02, 2016 at 02:55 PM
Hey, Im trying something similar and I got it working (PS sorry for the weird alignment, I cant seem to do anything about it):
GameObject rocket = Instantiate(RocketObject, rocketSpawnLocation.transform.position, Quaternion.identity) as GameObject;
Vector2 crosshairLocation = new Vector2(crosshair.transform.position.x, crosshair.transform.position.y);
Vector2 spawnLocation = new Vector2(rocketSpawnLocation.transform.position.x, rocketSpawnLocation.transform.position.y);
Vector2 direction = new Vector2(crosshairLocation.x - rocket.transform.position.x, crosshairLocation.y - rocket.transform.position.y);
direction.Normalize();
direction *= (rocketSpeed * 100);
rocket.GetComponent<Rigidbody2D>().AddForce(direction);
NetworkServer.Spawn(rocket);
Destroy(rocket, 2.0f);
Your answer
![](https://koobas.hobune.stream/wayback/20220613201424im_/https://answers.unity.com/themes/thub/images/avi.jpg)