Right now I have a first person character with a movement script attached. I use a rigid body and a character controller. The camera controls the transformation of the parent object with the the player object, in this case a capsule.
The player object has the movement script on it, and the camera has a camera controller on it, that just allows the mouse to control the transformation of the player object.
When I jump, my script does so the player keeps the momentum, and can not stop mid air, but if I turn my camera, the player object also changes trajectory depending on the transformation. If I look right, while in mid air, my jump also follows the camera to the right. I want the jump motion in mid air, to keep the original trajectory. Can someone help me to add that function, but with the camera still able to look around?
My movement script:
void movement () {
moveVector = Vector3.zero;
moveVector.x = Input.GetAxisRaw("Horizontal");
moveVector.z = Input.GetAxisRaw("Vertical");
if (controller.isGrounded) {
verticalVelocity = -1;
if (Input.GetButtonDown("Jump")) {
verticalVelocity = jumpforce;
}
} else {
verticalVelocity -= gravity * Time.deltaTime;
moveVector = lastMove;
}
moveVector.y = 0;
moveVector.Normalize ();
moveVector *= playerspeed;
moveVector.y = verticalVelocity;
worldMove = transform.TransformDirection (moveVector);
controller.Move (worldMove * Time.deltaTime);
//controller.Move (moveVector.z * transform.forward * Time.deltaTime);
//controller.Move (moveVector.x * transform.right * Time.deltaTime);
//controller.Move (moveVector.y * transform.up * Time.deltaTime);
//controller.Move (moveVector * Time.deltaTime);
lastMove = moveVector;
}
Its the:
moveVector = lastMove;
that does so my player does not stop mid air.
Also it would be amazing if it was possible to do so you cannot change direction mid air, but you can drag the trajectory a bit, but in a smooth way, like in Counter Stike: Global offensive and so.
Update: Here is my camera code:
public class CameraController : MonoBehaviour {
Vector2 mouseLook;
Vector2 smoothV;
public float sensitivity = 5.0f;
public float smoothing = 2.0f;
GameObject character;
// Use this for initialization
void Start () {
character = this.transform.parent.gameObject;
}
// Update is called once per frame
void Update () {
var md = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
md = Vector2.Scale (md, new Vector2 (sensitivity * smoothing, sensitivity * smoothing));
smoothV.x = Mathf.Lerp (smoothV.x, md.x, 1f / smoothing);
smoothV.y = Mathf.Lerp (smoothV.y, md.y, 1f / smoothing);
mouseLook += smoothV;
//Låser kameraret så man ikke kan kigge længere ned eller op end 90 grader
mouseLook.y = Mathf.Clamp (mouseLook.y, -90f, 90f);
transform.localRotation = Quaternion.AngleAxis (-mouseLook.y, Vector3.right);
character.transform.localRotation = Quaternion.AngleAxis (mouseLook.x, character.transform.up);
}
}