Rotation Explanation (rotate player while aiming)

Hey, all

There’s something puzzling me about a couple of scripts that I’d like you to explain, please.

I’m working on a third-person shooter game and right now I’m kinda stuck. At the moment I’m working on the camera by looking at the Bootcamp scripts and rewriting the code to suit my case. So far I’ve managed to position the camera slightly to the right, pan it up a little and make it zoom(aim) when I click the right mouse button.

Next what I wanna do is rotate the player while aiming. I’ve found the code that rotates the soldier while aiming but I just couldn’t adapt it for some reason.

Btw, I’m using the construction worker prefab to test my scripts.

Below I will try to explain how I think the code works. Note that I’m not sure it works this way; I just assume it does.

OK. First we have this little peace of code SoldierCamera.js:

 if(orbit  (Input.GetKeyDown(KeyCode.O) || Input.GetAxis("Horizontal") != 0.0 || Input.GetAxis("Vertical") != 0.0 || soldierController.aim || soldierController.fire))
    {
        GoToOrbitMode(false);
    }
    
    if(!orbit  soldierController.idleTimer > 0.1)
    {
        GoToOrbitMode(true);
    }

which I assume tells the camera when it can orbit around the player and when it can’t.

In this function:

 function RotateSoldier()
    {
        if(!orbit)
            soldierController.targetYRotation = x;
    }

I assume it’s telling it to access the SoldierController.js script and get the targetYRotaion variable. What I don’t understand here is why it equals x instead of y.

And finally we have this code from SoldierController.js:

var currentAngle = soldierTransform.localRotation.eulerAngles.y;
var delta = Mathf.Repeat ((targetYRotation - currentAngle), 360);
if (delta > 180)
    delta -= 360;
soldierTransform.localRotation.eulerAngles.y = Mathf.MoveTowards(currentAngle, currentAngle + delta, Time.deltaTime * maxRotationSpeed);

This, I can only assume, is what makes the soldier rotate. Looking at it I don’t see any variables dependent on anything else in the script (except soldierTransorm and maxRotationSpeed which are predefined variables), so I don’t understand why copying it and adapting it accordingly doesn’t work.

Of course I’m pretty sure I am wrong somewhere so I’d like someone to explain to me how this stuff works.

Thanks to anyone and everyone taking the time to help me.

Anyone?

It can’t be that hard to explain, right?