- Home /
Network.Instatiate Problem
Hi. I am doing a multiplayer fps, and so far everything is working fine over the network, except the shooting. To start off, I made my character shoot little spheres. I used this code:
using UnityEngine;
using System.Collections;
public class FP_Shooting : MonoBehaviour {
public GameObject bulletPrefab;
public float bulletImpulse;
private GameObject bulletParent;
Camera playerCamera;
// Use this for initialization
void Start () {
playerCamera = GetComponentInChildren<Camera> ();
bulletParent = GameObject.FindGameObjectWithTag ("CloneHolder");
}
// Update is called once per frame
void Update () {
if (!networkView.isMine) {
return;
}
if(Input.GetButtonDown("Fire1")){
GameObject bullet = (GameObject)Network.Instantiate(bulletPrefab, playerCamera.transform.position, transform.rotation, 0);
bullet.rigidbody.AddForce(playerCamera.transform.forward * bulletImpulse);
bullet.transform.parent = bulletParent.transform;
}
}
}
The problem is that, over the network, the ball always comes out of the same side of the player, regardless of his rotation, which IS clearly updating. I tried adding a network view to the camera and change the rotation parameter on the Network.Instantiate to playerCamera.transform.rotation, but that did not work as well. What should I do?
Using Network.Instantiate
or even the rigidbody system for bullets is simply a bad idea. For something as common as shooting a bullet you should use an RPC function ins$$anonymous$$d.
And how would I implement that? Could you link me to any tutorials please?
You can't do a search for "Unity RPC" yourself? For bullet physics I can't recommend learning projectile motion enough. I'd go with a ray-casting or even sphere-casting approach.
You might find this link helpful: great vid series on multiplayer FPS. http://www.gamertogamedeveloper.com/
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Multiplayer projectiles 0 Answers
Photon Networking Instantiating Problem 0 Answers
Instantiate object from client doesn't work on server. 1 Answer
How can I spawn a GameObject on all players in my multiplayer game? 0 Answers