Unity3d Photon character movement not synchronous - unity3d

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.

Related

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

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)

Unity Hand tracking issue on the quest with UI raycasting

I'm working on a Unity project, trying to test the UI interaction on the Quest II with hand tracking and ray casting.
I've set up a very basic scene with all the default assets (OVRCameraRig, UIHelper) and added just a button to test the UI interaction.
This is what my scene looks like:
The issue I have is, when I run the scene, the Ray is rotated 90 degrees, and it's attached to the wrist for some reason. I made a video here to show what I mean :
https://www.youtube.com/watch?v=5f12yfpugB8
It's still interacting with the UI though.
Then after watching some online tutorials, I commented out these lines in the HandInputSelector.cs, which was attached to the UIHelper:
void SetActiveController(OVRInput.Controller c)
{
/*
Transform t;
if(c == OVRInput.Controller.LTouch)
{
t = m_CameraRig.leftHandAnchor;
}
else
{
t = m_CameraRig.rightHandAnchor;
}
m_InputModule.rayTransform = t;
*/
}
and instead added a 2nd script to the UI helper, with these lines only:
public OVRHand hand;
public OVRInputModule inputModule;
private void Start()
{
inputModule.rayTransform = hand.PointerPose;
}
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
Now the ray is at least attached to the correct position, but it still doesn't rotate properly with the hand movement. I made another video of it here :
https://youtu.be/q3d0eG2LwY0
My Unity version is 2021.3.1f1
Can someone please tell me what I'm doing wrong?

How would I detect if my player is touching a game object?

Okay so basically im working on this test project that relies heavily on movement and momentum to complete levels, with a bit of parkour. I need a collider for two reasons, 1. The player touches an object at the end and gets put back into the menu. 2. If the player falls out of the map they die.
I tried a bit messing around with Colliders and at the Docs and while usually id figure it out ive been stumped for 20 minutes looking at Unity's docs and a few questions from here.
public GameObject objectCollider;
public GameObject anotherCollider;
void OnCollisionEnter(Collision collision)
{
if (CollisionDetection.IsTouching(objectCollider.gameObject, anotherCollider.gameObject))
{
Destroy(plr);
}
}
This is what I got so far. I get an error from here and if I switch it to if collider object == the other it errors out.
Basically what I want (If you perfer to just post the answer code but comments on it would still be helpful so i learn!) is for one gameobject (player (but in code its objectCollider)) to be detected if it touches the other (a cube (in code its anotherCollider)) and to execute code (for example Destroy(playerObject))
Thank you for any help you bring here, links, code anything!
Hopefully this is what you're looking for:
public void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "endObject")
{
//put back into the menu
}
}
Once your player enters a collision, it checks the name of whatever object it collided with. Then you can execute what code you want in the if statement.
You can use collision.collider for many other things, such as collision.collider.tag, but this should give you a start.

Unity - Parent with children fall apart

I have a player character, made of some cubes, spheres and a capsule. I created the empty object Player and all body parts of the player are a child of Player. I have two planes, with a moving platform in between. I can walk and jump on the normal planes and the walls, but when the player is on the moving platform the bodyparts of the player fall apart. Maybe it's something really stupid, but I just started with Unity.
This is what goes wrong, the player falls apart on the moving platform: http://nl.tinypic.com/r/207s3sz/9
And below the information about the overview, the player, the body parts, and the moving platform with according character-holder. All bodyparts have the same properties as the body part on the screenshot. Can anyone help me with what goes wrong here? How can I transport the whole player by the moving platform?
HoldCharacter script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HoldCharacter : MonoBehaviour {
void OnTriggerEnter(Collider other) {
other.transform.parent = gameObject.transform;
}
void OnTriggerExit(Collider other)
{
other.transform.parent = null;
}
}
You just need to disable the isTrigger flag. Here are some insights
so how Is Trigger works is that... it will fire OnTriggerExit and OnTriggerEnter, but it will let the object go through it. If you disable the IsTrigger, then you need to move the logic to OnCollisionEnter on OnCollisionExit methods. If the isTrigger uncheck kind of worked, maybe is just the fact that you move the logic for HoldCharacter to OnCollisionEnter and OnCollisionEnd respectively Like this:
void OnCollisionEnter(Collision collisionInfo) {
collisionInfo.gameObject.transform.parent = gameObject.transform;
}
void OnCollisionExit(Collision collisionInfo) {
collisionInfo.gameObject.transform.parent = null;
}
Regards
If I'm correct, the children of objects with Rigidbodys have physics as well. Maybe put the rigidbody on a child of the player, like so.
Player
-head
-arms
-legs
-empty gameobject with rigidbody

unity3d OnMouseDown function

I am new to Unity3D. I am trying to do a simple thing. But not able to do this. I have a .obj file which is a 3d key file. I do the followings:
Import this key (to assets) in unity3D
Add this key to scene (from assets to hierarchy)
Add a script to this key
Add the OnMouseDown() function to this script as follows -
void OnMouseDown()
{
Debug.Log ("clicked...");
}
But when I click the key no message is showing in console. Please tell me what is the problem?
make sure the gameobject is not at layer "Ignore Raycast"
Use the following inside your update function to see raycasting is working fine.
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Debug.Log ("Name = " + hit.collider.name);
Debug.Log ("Tag = " + hit.collider.tag);
Debug.Log ("Hit Point = " + hit.point);
Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
Debug.Log ("--------------");
}
}
}
The OnMouseDown and it's related functions are all actually older systems. The newer system to use for this is the event system.
Specifically to get mouse clicks you would implement this interface in a class:
http://docs.unity3d.com/460/Documentation/ScriptReference/EventSystems.EventTriggerType.PointerClick.html
But there is actually an easier way to do it without code.
Using this component:
http://docs.unity3d.com/ScriptReference/EventSystems.EventTrigger.html
You can then visually forward the mouse click event to something else.
If you don't fully follow what I have said so far, the thing to really start at is learning the UI system. You can find video tutorials here:
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-canvas
And just to make it clear, the UI system is not just for UI. The UI system runs off an EventSystem which is what you want to use to forward input events to any object 3D or 2D in the entire scene. The UI tutorials will explain the usage of that EventSystem.
You need to assign a collider to the 3d object if you want to fire it with OnMouseDown:
OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
This event is sent to all scripts of the Collider or GUIElement.
You can also do like by override click function
public override void OnPointerClick(PointerEventData data)
{
Debug.Log("OnPointerClick called.");
}