İn Unity Photon Network , Learn who did the bullet come from? - unity3d

I make a game using photon network . 2 actors are shooting at each other and when the bullet is formed on the stage I want to know who the bullet came from. I can send player id in bullet Instantiate and I can find player in for loop but i don't think it's true.
Is there better than this method?
Code
void Shoot()
{
var part = GetComponentInChildren<ParticleSystem>();
part.Play();
float angle = cc.isFacingRight ? 0f : 180f;
GameObject gameob = PhotonNetwork.Instantiate("Bullet", firingPoint.position, Quaternion.Euler(new Vector3(0f, 0f, angle)),0, null);
}

You can simply use photonView.Owner, since the bullet is a networked object. Alternatively, you can use photonView.IsMine to check if the client running the check owns the bullet, instead of comparing owners.
(The photonView instance comes from assuming your bullet script extends MonoBehaviourPun instead of MonoBehaviour)

Related

Why does my bullet lack a rigidbody even though it has one?

So I'm making a FPS game and when I try to shoot, it says my bullet lacks a rigidbody. I have a a rigidbody, so why is it like this?
Here's the bit of code that makes the bullet :
Vector3 aimDir = (transform.forward).normalized;
Instantiate(gunData.bulletPrefab, muzzle.position, Quaternion.LookRotation(aimDir, Vector3.up));
Rigidbody bulletRb = gunData.bulletPrefab.GetComponent<Rigidbody>();
bulletRb.velocity = aimDir * gunData.bulletSpeed;
You seem to be trying to access the bulletPrefab's rigid body component instead of the newly instantiated object's.
try something like this:
var myNewBulletInstance = Instantiate(....
myNewBulletInstance.GetComponent<Rigidbody>();
now It works. I also had to create a prefab variant to put the rigidbody on. thanks!

How to move an object in Unity

Hello guys I try to make a city building game the idea is very simple actually Instantiate an Building and move them with mouse after that hit the place button and place.
The issue is if the collider of a another building completely encloses the collider of the building that I am built I can't move new building.
I can probably explain better with pictures.
Issue 1
The first picture my new building you can see collider limits and the second one my old building that I already placed. I Understand the problem but I can not solve it.
And here it is my object drag code
private void OnMouseDrag()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, 1000f,(1 << LayerMask.NameToLayer("Ground"))))
{
Debug.Log(hitInfo.transform.name);
transform.position = SnapToGrid(hitInfo.point);
}
}
private Vector3Int SnapToGrid(Vector3 pos)
{
Vector3 tempPos = pos;
Vector3Int snappedPos;
snappedPos = new Vector3Int(Mathf.RoundToInt(tempPos.x), Mathf.RoundToInt(tempPos.y), Mathf.RoundToInt(tempPos.z));
return snappedPos;
}
Thanks for the help in advance
If I got your question right, I believe that you can solve the problem by enableing the "Is Trigger" option of the previously placed buildings colliders at the moment that a new building is being moved around. Enableing this option in an object makes it so other objects can pass trought it when collision happens.

Updating values in a component in instantiated gameobject in unity

Hi i have a Robot Prefab, with a NPC script that is linked to dialogue sentences.
I am retrieving the information about dialogues (using a loader script) from a server API, and then instantiating both the robot gameobject and the sentences. For example for instantiating the gameobject:
GameObject newObject = GameObject.Instantiate(Resources.Load("prefabs/" + item.gameObject.name) as GameObject, parentProps);
newObject.transform.localPosition = new Vector3(loaded.x, loaded.y, loaded.z);
newObject.transform.localEulerAngles = new Vector3(0, 90f, 0);
However, i do not know how to access the dialogue and update it. Could someone tell me how to update the instantiated value of the sentences here (marked it with red arrows on the image below)?
May be a basic question, but am new. Thanks!
You can create a public function in Npc class and call it after you instantiated it.
GameObject newObject = GameObject.Instantiate(Resources.Load("prefabs/" + item.gameObject.name) as GameObject, parentProps);
newObject.transform.localPosition = new Vector3(loaded.x, loaded.y, loaded.z);
newObject.transform.localEulerAngles = new Vector3(0, 90f, 0);
newObject.GetComponent<Npc>().UpdateDialog();
On your Robot GameObject use GetComponent to get the NPC script object. Then you are able to modify the values. Make sure that the visibility of your variables is either public or has getters/setters.

Using RPC to spawn my player

In my project, I have two prefabs - One with a first person camera, and another one stripped of all it's first person components such as the mouselook, input controller, camera, and so on. My ultimate goal is to spawn my prefab with the camera on it and sending an rpc to spawn the dummy prefab on all other clients to sync up with my first person prefab. The problem I'm facing is when I try to send the rpc from my first person prefab's PhotonView, the rpc doesn't get called on other clients because the PhotonView id doesn't exist on other clients yet.
The following code is located on my NetworkManager script on an empty game object in the hierarchy.
void OnJoinedRoom(){
GameObject MyPlayer = (GameObject) Instantiate (FPSPlayer,spawnPoint.transform.position, spawnPoint.transform.rotation);
FPSPlayerPhotonView = MyPlayer.transform.root.gameObject.GetPhotonView();
int id = PhotonNetwork.AllocateViewID ();
FPSPlayerPhotonView.viewID = id;
FPSPlayerPhotonView.RPC("SpawnMyPlayerAsRemote", PhotonTargets.OthersBuffered,id, spawnPoint.transform.position, spawnPoint.transform.rotation);
}
[PunRPC]
void SpawnMyPlayerAsRemote(int id, Vector3 pos, Quaternion rot){
GameObject MyRemotePlayer = (GameObject)Instantiate (RemotePlayer, pos,rot);
RemotePlayerPhotonView = MyRemotePlayer.transform.root.gameObject.GetPhotonView();
RemotePlayerPhotonView.viewID = id;
}
This method is tested and will work.
What I suggest you do is create an empty player with NetworkView.
Create this player via PhotonNetwork.Instantiate so it will be instantiated inside all clients.
Check you are the owner of the networkview.
If you are, create all the client sided essentials (FPS Camera etc...).
If you aren't, create the dummy.
EDIT: I was wrong on the RPC part.

Unity3d Photon character movement not synchronous

I am beginner to photon on unity3d. I want to move character synchronously in a game. I am attaching script as an observer to the photon view and using this code
void OnPhotonSerializeView(PhotonStream stream,PhotonMessageInfo info)
{
if (stream.isWriting)
{
Debug.Log("writing");
stream.SendNext(transform.position);
stream.SendNext(transform.rotation);
}
else
{
Debug.Log("reading");
this.correctPlayerPos = (Vector3)stream.ReceiveNext();
this.correctPlayerRot = (Quaternion)stream.ReceiveNext();
}
}
The problem is that the player who creates the room can change the position and rotation of player, it can only write. But the second player(who joins the rooms) cannot change the position and rotation,it can only read.
What can be the problem with my setup.
I have followed the marco polo tutorial(http://doc.exitgames.com/en/pun/current/tutorials/tutorial-marco-polo) for this. Any help is highly apperciated.
If you instantiate a GameObject per player via PhotonNetwork.Instantiate, then each client has it's own(ed) GameObject which this client can write updates for.