Event Triggers to move player not working in multiplayer on mobile device.
Basically everything worked fine on my cube player before I started trying to get a multiplayer demo going. I got multiplayer connecting and I can see the other cube player moving around. Except, my touch buttons on my screen no longer work on the mobile device. I poked around and it is because I have Event Triggers set to functions that move the cube around. If I pause unity and manually add the Player(clone) and do the Event triggers that way, the cube can be moved again. If I attach the Canvas with the UI buttons to the player, then when the player spawns, the controls work again, but they rotate with the player as it moves. I have tried a script to detach the child after spawning, and that works also, problem now is that when another player joins the game, their canvas spawns and detaches also, so there are 2 canvases overlapping each other on both screens, making one of them unreachable. I have tried to instantiate the canvas with the UI buttons after your player spawns, and while it does instantiate the canvas correctly, the buttons again do not work. This is driving me crazy! What is the proper process here?
@magnomous @mateo4632 You two had similar questions but I don't know how to contact a user on unity. Wondering if you could help with some input.
You just need to assign events of the Event Trigger via code. They won't be visible in the inspector, but they are in there. This is a part of the code from the game I coded. It's triggered once you tap the screen and calls the function to move your character:
EventTrigger.Entry entry1 = new EventTrigger.Entry();
entry1.eventID = EventTriggerType.Drag;
entry1.callback.AddListener((eventData) => { GetComponent<Player$$anonymous$$ovement>().OnJoystickDrag(); });
joystickDragArea.GetComponent<EventTrigger>().triggers[2] = entry1;
Answer by dhotlo2 · Nov 03, 2016 at 01:32 PM
@Magnomous So I have to assign events with code instead of doing it in the inspector, but I am having a bit of a problem understanding the code fully. For my game, there is a left and right button that adds a force to a cub to make it move. On the Right button for example, there is an event trigger to call the RightClick() function which applies a force on pointerdown. Do I put this code on the player movement script then? Could you add some comments to each line maybe? I have buttons so I'm assuming I don't need to have OnJoystickDrag(); in there but something else?
It really depends on what kind of game you have. Assigning events in code is viable pretty much only when they need to be assigned during runtime. Do you instantiate this cube at runtime or is it already in the scene? $$anonymous$$aybe all you need to do is something like this:
rightButton.GetComponent<Button>().onClick.AddListener(() => { go.GetComponent<PlayerController>().RightClick(); });
"rightButton" would be a gamobject containing a Button component. "go" would be a gameobject with your script that contains RightClick funciton - in my example the script would be called PlayerController.
The cube player gets spawned when the game starts, it is a multiplayer game, so when you join a session, your player gets instantiated.
@$$anonymous$$agnomous I see. I don't understand why the code is all red still and not recognized?
@$$anonymous$$agnomous ok so one more quick question and I will leave you alone since you made a multiplayer mobile game. What should the optimal settings be in the Inspector on the player for Network Transform (script)? As in the movement threshold , snap threshold, interpolate movement. I looked up a few of them and I understand what some are, but others are very confusing. What should the settings be for I guess Optimal syncing of the Player Cubes (They are rigidbodies and move with AddForce).
Well, I used Photon networking and since you are using Network Transform, I suppose you use the standard unity networking, which I don't have experiences with. But generally you need to guess what optimal settings should be and test whether they suit you. I think you should also check some youtube tutorials about unity networking. They would probably give you a good understanding what exactly these settings do.
I guess to elaborate more on my Specific Example: I have a canvas with the UI Buttons on screen. Then I have a player prefab that has the playermovement script on it. In the Playermovement script I have functions to move the player with force that use bools to trigger the movement on and off, based on whether a button is touched or not. So for example the right button movement looks like: public void RightClick(){ isRightClicked = true; } and then in the Update function I have :
void Update(){ If (isRightClicked) { rb.addforce(5,0,0); } }
Where would I input the event trigger code in my example? Can I assign the event triggers in the Start() function , so that they get assigned for each player as they spawn?
Is it a networked, multiplayer game? If so, then yes, I think you can assign it in the Start(), but you should also include something like if(photonView.is$$anonymous$$ine), then assign this button to $$anonymous$$e, and only $$anonymous$$e cube (you don't want to make an input to the cubes that you should not be able to control, do you?)- that would be for Photon networking. And the onClick assignment could look like:
rightButton.GetComponent<Button>().onClick.AddListener(() => { go.GetComponent<Playermovement>().RightClick(); });
and the "go" would be a GameObject - your cube. RightButton would be a child object of the Canvas - the rightbutton GameObject.
It's all red when i put that in the Start function. Also I am using Pointer Down ins$$anonymous$$d of onClick , and I was reading that your line of code only works for onClick?
Your answer
Follow this Question
Related Questions
Multiple mobile devices 0 Answers
i need help with my scipt ( photon mobile)!!!!! 0 Answers
Best way to set up mobile multiplayer 0 Answers
OnPointerDown for iOS delayed? 1 Answer
Login register in android (MAMP) 0 Answers