- Home /
Trying to get the camera to orbit, but also steer the player when moving.
As the question above states, I have a script for controlling the main camera, and one for controlling the player. I want the two to somehow interact. Now, first, explanation. The main camera script gets the player's transform. It orbits the player by horizontal mouse movement. That works. The player moves forward/back on vertical input (w/s on the keyboard, forward/back on the joystick's main stick). That works.
What does not, is trying to get the player to turn left/right to mouse movement while moving forward, but still have the camera orbit when they stop. Right now, if you leave the mouse perfectly still, you can move back and forth. But if you move the mouse while moving the player, the camera spins the direction you move the mouse. And the farther you move the mouse left/right, the worse the spin becomes. Here's the code:
if ( Mathf.Abs ( forwardInput ) > inputSettings.inputDelay )
{
// rotationAngle is a Vector3
// make character move forward relative to camera's aim
// first get our current player's transform angles into a variable
// I had deleted the code for a test, this next line may not be accurate
rotationAngle = transform.eulerAngles;
// Then, read out camera angle to the variable's y component
rotationAngle.y = Camera.main.transform.eulerAngles.y;
// Finally, update the character's angles with the new y component
transform.eulerAngles = rotationAngle;
// move here
velocity.z = moveSettings.forwardVelocity * forwardInput;
}
Then later in FixedUpdate():
// characterBody is the player character's rigidbody
characterBody.velocity = transform.TransformDirection ( velocity );
All I was trying to do is get the character to face the same way the camera is pointing, so that when the velocity is updated, the character would also be facing the same way the camera is. Which means during movement, it should turn the character when you turn the camera with the mouse. But it keeps spinning like it can never get to the camera's position. Also I have a mouse running at 8200 dpi and everything mouse based in Unity is FAR too fast, but I can't figure out how to slow it down. There doesn't seem to be a 'mouse sensitivity' setting for the project. :(