I'm currently using the MouseOrbit.js script from the standard assets to allow the player to rotate the camera around the main character during the game. I'd now like to have the camera reposition itself behind the player character when she moves, so that the camera always faces in the direction the player character is moving.
I found a fascinating thread on the forums where they were attempting to recreate the camera system from WoW. I haven't played it myself, but from the descriptions it sounds like the camera repositions itself behind the player character. In particular i've borrowed a couple of lines from that script and amended my MouseOrbit script (specifically the if statement in the LateUpdate function):
It works but with two problems:
1) When I press Play, the camera jumps to a rotation looking at the player depending on where the mouse pointer is on screen when I start the game, rather than initially starting behind the player.
2) When I move the player character, the camera repositions itself in front of the player character rather than behind. The values must be reversed in some way, I need to figure out how to flip them.
I make a point of never using a script unless I at least understand what it does (preferably how it does it) since I don't subscribe to the "write my script for me" ethos. But I have to admit I don't really understand what's going on here. If I did, i'd probably be able to work out myself how to fix the two problems above.
If someone could enlighten me, i'd be extremely grateful.
asked May 07 '10 at 06:38 PM
(1) is likely some function of your default camera transform... I'm not seeing anything in the update script that does more than apply an offset to the existing camera position/rotation. That Else clause just takes the amount the mouse has moved this frame, scales it a bit, and adjusts the script's own notion of what the camera x & y rotations should be. Check that the camera as it exists in your scene prior to running the game is in the position and facing the direction you want, and isn't being affected by any scripts before this one, because up in Start, this one sets its own internal x and y to the camera transform as of application start. Alternatively, you may try moving the first 3 lines of Start into the top of LateUpdate so when LateUpdate does its x+=/y+= stuff, it's offsetting the most recent camera position. This will, however, screw up the auto lerp-angle generation in the first part of the If clause, and consequently screw up your follow-target-motion effect :P
(2) may indicate your models are set up in a different axis orientation than the target used by the original author (e.g. your models face X+, theirs faced X-). Just add +180 to the targetRotationAngle and CurrentRotationAngle lines, or alternatively, just after the LerpAngle() where x is assigned. I'd imagine that does it.
answered May 07 '10 at 07:32 PM