- Home /
Issues with camera and player rotation scripts for basic FPS controller.
Hello! I'm new to Unity and game development in general. As of now, I'm trying to make a simple 3rd person shooter game. I'm starting off by creating a simple scene for testing, adding a player, giving it movements and adding a camera.
I find movement and camera very important which is why I refrain from using advanced prefab or over developed scripts, as they often come with additional functions that I don't want or need. Currently, I'm using these scripts for my player game object:
Movement - http://pastebin.com/rfGTV6iT (http://docs.unity3d.com/ScriptReference/CharacterController.Move.html)
MouseLook - http://pastebin.com/vW15FcpL (SimpleMouseRotator.cs accessible in the standard assets->utility scripts)
So, here are my current issues:
Issue with movement script: pushing keys that would move the player in the opposite direction doesn't cancel movement (eg. pushing and holding down W and then S doesn't stop the player from moving forward). I would like the player to stop walking, and upon releasing W with S still held down, move backwards.
Also I would like it to be possible to move the player slightly while in the air. Sort of like air acceleration. What is a good way of doing so? Adding a rigidbody-component and adding force to a player whilst isGrounded = false?
With the current MouseLook-script, the player model rotates horizontally which is all good. When aiming higher or lower however, the player model starts flipping around like crazy. I would like the player model to remain in an upright position but for the camera (and hypothetical crosshair/target) to follow the mouse movements. Any help is appreciated!
Answer by ke1th · Jul 31, 2015 at 02:54 PM
I found a solution for the third issue (the one where MouseLook rotation of player caused the player to fall). This was to give player the MouseLook to work horizontally and the camera to only work vertically. No idea if this is appropriate or if it will cause problems further in. :d
Answer by mihai_pruna · Apr 17, 2016 at 12:10 PM
This is for Unity 5.3 Here's a simple script to get a first person controller mouse look to work sort of OK, like in a FPS game. I used this with a rigid body FPS controller script attached to the camera.
In mouseLook.cs, change the beginning of the LookRotation function, up to and including the line where it sets the m_CameraTargetRot:
public void LookRotation(Transform character, Transform camera)
{
float yRot = CrossPlatformInputManager.GetAxis("Mouse X") * XSensitivity;
float xRot = CrossPlatformInputManager.GetAxis("Mouse Y") * YSensitivity;
//camera rotates on its own local axes
//get the camera forward direction in relation to the world
Vector3 fwd = camera.forward;
fwd.Normalize ();
//rotating around the y and z local axes depends on camera forward direction
float newyrot = yRot *(float) Math.Sqrt (fwd.x * fwd.x + fwd.z * fwd.z);
m_CameraTargetRot *= Quaternion.Euler (-xRot, newyrot, yRot*fwd.y);
Your answer
Follow this Question
Related Questions
Noob: Question about standard c# mouselook script. And other probably noob questions others may ask. 3 Answers
Player not rotating with camera 1 Answer
Multiplayer Mouselook on Soldier (FPS) 0 Answers
click to move workig script!! but pls help with rotation!! :( 1 Answer
How to make a RigidBody not go into ground when tilted foward? 2 Answers