- Home /
Make player face same direction as camera.
I currently have a camera that can be rotated around the player by holding shift and moving the mouse. The only problem is that now I want the player to move in a direction relative to the direction camera is facing slowly and to rotate to face the same direction as the camera.
I hope that was clear enough to understand :|
Answer by Althaen · Nov 18, 2012 at 01:30 AM
Just decided to rotate the player with the camera attached to it. Works fine so far.
Answer by sparkzbarca · Nov 16, 2012 at 04:51 AM
so you want to rotate towards something?
How about the RotateTowards function :)
if you dont want to set a max speed you could just use slerp feed it the from and the to and it'll make it happen
transform.rotation = quanternion.slerp(quanternion from, quanternion to);
now you still need to get the to and the from and have them in quanternion form.
we know we want to rotate from the front of the player to the front of the camera.
actually since you want the the front to face the front you really just want the players rotation to match the cameras. So we can shortcut getting vectors and turning them into quanternions and go straight to
transform.rotation = quanternion.slerp(player.transform.rotation, camera.transform.rotation);
you want to move in a direction relative to the camera.
Well direction is a vector. and the vectors relative to a gameobject are stored in its transform.
camera.transform.forward is a vector which points from the center of the camera directly forward (it can be thought of as passing through the point in the middle of the screen.
-camera.transform.forward is the inverse. A negative sign reverses the direction basically. forward becomes backward.
with camera.transform.right,camera.transform.up,camera.transform.forward and the negative sign we can get all 6 directions of movement and we can move an object in those directions.
if we'd like we can normalize those vectors. A normalized vector is basically the slope of the line.
its as if the center of the object is 0,0,0 and not wherever it is in world space.
if we just add that vector to an existing position we move in the same direction as that line. except with respect to the current objects position.
So we could do
player.transform.position += camera.transform.forward.normalized;
and bam its moving parallel to the camera.
there are other ways as well you dont have to normalize you can you transformdirection but the end result is the same. Look up transformdirection in unity script reference for more details on that.
Mark as answered.
How can I lock the characters rotation to up? Because my camera sits at an angle and now my player faces to floor and the camera spazzes out.
Answer by Coreyf716 · Nov 18, 2012 at 01:31 AM
I just use the horizontal mouse look in the player controller script and a seperate vertical mouse look script attached to the main camera.
Answer by GarbageElitist · Nov 18, 2012 at 04:30 AM
3DBuzz actually has a third person controller video tutorial series that goes into this a bit.
http://www.3dbuzz.com/vbforum/sv_videonav.php?fid=6d5630dac2c391b9e563c4693d44d7cc
It's sort of long, but since you seem fairly experienced, you could always skip to the parts where they talk about camera. Their player snaps to camera view, much like if you held down right click in an MMO.