- Home /
Camera movement independent of player control
My question is...
How do I script for using a mouse to look around without affecting the players rotation?
Using the mouseorbit script does what I expect on it's own, but once a player movement control script is assigned, they affect each other.
Depending on what objects I assign the scripts to, with the player sitting in a cockpit, I either get the mouseorbit moving the cockpit and the flight controls doing nothing, or the mouseorbit works but the cockpit revolves around where the camera is. Does that make sense?
From the initial view looking forward through the canopy, I need to be able to say, look left with the mouse and have the cockpit stay in that position while I carry on flying.
Thanks for looking anyway even if you don't understand what I'm babbling about :)
Answer by sevensixtytwo · Sep 28, 2014 at 12:31 PM
It's actually quite simple and pretty much depends on how you structure your player object's heirarchies.
For what you're looking for, I may suggest building your "Move" script to use the ship's relative transforms rather than the camera's and then attaching your camera (with its "Look" script) to the ship as a child. In essence, the ship moves, and the camera goes along for the ride.
Hierarchy: Object (Moves and Turns) -> Camera (Looks)
Here's an example of how I used this type of structure: https://dl.dropboxusercontent.com/u/282973338/WebBuild/WebBuild.html
Just tap keys 1-4 to switch positions. Use the mouse to look around and WASD to turn and move the tank.
You can even get creative and do as ARMA does: Mouse turns the entire ship, keeping a button pressed locks the ship and lets you look around the cockpit.
Thanks for replying. Yes, that tank thing is very similar. I'm not sure how I can structure things any different though. Currently, I have a gameobject called "Cockpit". Assigned as a child is a group containing the relevant meshes, then another child is a "head" gameobject which has it's own child of the camera. The control script was found here somewhere and asks for "flyer" variable, which I presume to be the "Cockpit" object. The "mouseOrbit" script asks for "Target" variable, which I am again presu$$anonymous$$g, to be the camera (or the "head" object). I can only assume that I'm misunderstanding the "flyer" and "target" variables...or I need to adjust the scripts a certain way. The controls script was found here...link text
The mouseorbit is the included Unity script (I think)
Yes, well, I would suggest using the $$anonymous$$ouseLook script rather than $$anonymous$$ouseOrbit and putting it on your "head" or your camera. Your flyer script can be put on your "Cockpit" and have it reference itself.
Unless you added mouse Input to the control script, the camera should now be able to look around independently from the rest of the ship.
Thanks, i'll try the mouselook ins$$anonymous$$d and see. I'm beginning to think that flightcontrols script doesn't work properly anyway, as if I add objects to the scene, like cubes just hanging there to shoot at, they seem to move with the controls as well (to a certain extent)...I have no idea why, as they are not children of the cockpit object. Anyway, I'll try that suggestion now
Cheers mate, look and not orbit it is :) I think when I started, that the mouse was controlling the ship and looked to the orbit, rather than altering the inputs. As to the other objects moving, I don't think they are, just that the ship doesn't pitch with the correct pivot. I will ponder that for a while before posting another numptyish question :) Thanks again!
Your answer
Follow this Question
Related Questions
Unity Android Movement Problem 0 Answers
weapon bob when looking 2 Answers
how to find mouse look 1 Answer
How can I produce the typical FPS mouse steering effect? 2 Answers