Converting java to C#

Hi,

I am very new to coding. And I have been reading alot, I just havn’t actually started coding. So I figured I wasn’t learning anything because I wasn’t actually doing it. I decided to try to make a small test FPS by converting javascript over to C#.

So I tried to make a mouse(camera) controller for my FPS. And I have no idea what I did wrong. I’m sure there is plenty wrong… But I thought the best way to learn would be to hear what I did wrong instead of just guessing for hours.

Here is my code that I am having trouble with:

I think you want some code like this:

h += Input.GetAxis ("Mouse X") * horizontalSensitivity * Time.deltaTime;
v -= Input.GetAxis ("Mouse Y") * verticalSensitivity * Time.deltaTime;

v = ClampAngle (v, minVerticalAngle, maxVerticalAngle);
		
newRotation = Quaternion.Euler (v, h, 0.0f);
cameraTransform.rotation = newRotation;

Also if you want to add smoothing just add a new Quaternion variable and use Quaternion.Slerp function.

Hey thanks man, but I guess I wasn’t really looking for a new script. I was more so looking for an explanation of what was wrong with my script. I’m only really making this game to learn to code.

Yeah I wasn’t trying to give you different code to use. I was just showing you a way it can be done. The camera updates should be in LateUpdate() not Update(). Also you might want to make a new function like the one I have called ClampAngle. It should check if the angle is over 360 and subtract 360 from it, or if it’s lower than -360 it should add 360 to it. Then it takes the final angle and clamps it using Mathf.Clamp and returns it.

    public static float ClampAngle(float angle, float min, float max)
    {
        if (angle < -360F)
            angle += 360F;
        if (angle > 360F)
            angle -= 360F;
        return Mathf.Clamp(angle, min, max);
    }