- Home /
Splitscreen issue, second camera inverts controls.
Hi, I am currently working on a game of which contains splitscreen levels, using two Xbox 360 Controllers.
I have all input working, as well as a working splitscreen layout, however, I am stuck with an issue of which one out of the two cameras set up doesn't update the direction of the player properly, making it so that when focusing on the character on the screen with the issue, if you turned and moved, it would be inverted, but will uninvert in certain locations of the map and then back again.
I have looked into the code as I am using the default ThirdPersonController for both characters and I am currently using the SpringFollowCamera script too.
One thing I should point out is that if I change one of the two cameras's tag from MainCamera to something else, the one of which is still the MainCamera works with no issue. This happens the other way around with the other character and camera.
Something that I thought was worth noticing is that in the script for the ThirdPersonController, there is a section about updating the character's facing direction with the camera, and I have tried tweaking it with no luck, so I reverted it back to the way it was. Here it is:
function UpdateSmoothedMovementDirection ()
{
var cameraTransform = Camera.main.transform;
var grounded = IsGrounded();
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
var v = Input.GetAxisRaw("updn");
var h = Input.GetAxisRaw("leftnr");
// Are we moving backwards or looking backwards
if (v < -0.2)
movingBack = false;
else
movingBack = true;
var wasMoving = isMoving;
isMoving = Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1;
// Target direction relative to the camera
var targetDirection = h * right + v * forward;
// Grounded controls
if (grounded)
{
I am looking for somebody to help optimize this code so the one camera that is faulty is fixed, then I can finally move on and finish this feature.
Thank You very much for reading and I hope to get a reply as soon as possible, thanks.
Can you share what errors you get? Sometimes it might be these errors and not the actual logic that is preventing you from reaching the required output.
There are no errors in the compiler, just a faulty camera. I really need to know how to make a public camera field as thelacky3326 mentioned, then this problem is resolved :)
Answer by thelackey3326 · Dec 01, 2014 at 04:58 AM
Camera.main will only return the first camera found called "MainCamera". If you have two Third Person Control scripts running, and two GameObjects tagged as MainCamera, then one of the scripts will find the other object and not the camera it's attached to. You should probably augment the script so that you can assign the camera that you want it to control, i.e. make a public Camera field and drag the Camera to it in the inspector.
Thanks for the reply, I am relatively new to coding and I am unsure how to go about this exactly. I have tried to edit the above code in order to suit this but I keep getting errors, is there anything specific I can put into this code that you have in $$anonymous$$d? Thanks.
After reading this again I have thought it would be important to note that both characters are using two seperate identical scripts each in order to work, just renamed and reassigned, like I say all input works perfectly.
I have tried editing the .mainCamera code into something else but I cannot seem to find something that would work, what I meant by error is that nothing seems to happen, I should have been more specific. If there is anything anyone wants to suggest, please let me know, I need this finishing before Christmas.
Thanks.
Answer by joshcollins5 · Dec 02, 2014 at 07:23 PM
Problem solved, thanks for your answers. I managed to make a new public variable in the top of the controller script and put assign next to it.
I then put that variable.transform where the main.camera was and now it is fixed :D
Sorry I couldn't get back to you sooner! Looks like you have a solution, though. Good luck!