Check my code for improvements?

Hello!

I have writen code for simple camera effects (bobbing and side movements) and would love feedback.

I care the most (a lot) about performance, above all else. I have written this code and would like to know if there’s anything that can be improved. If not for performance, anything else?

Be as harsh as possible (though I don’t really care about naming stuff correctly). For example, if a anything can be made private, mention it.

I don’t usually do comments but I tried my best to help you go through the code faster.

Thank you so much in advance <3.

Bobbing.cs (3.4 KB)

Edit: I think the bobbing code is very framerate dependent (which I think is bad?). I’m running this at 240 fps. Are there smarter ways? I have heard that you should avoid Time.deltaTime so I’m kinda lost at that.

Also, are memory leaks (like in c++) problems that exist here and I should be worried about?

You don’t need to worry about the performance of code for a single player controller. It’s only when writing code for enemy AI and your game has thousands of enemies running around will you be able to notice any difference between Vector3.Distance, Vector3.Magnitude, Vector3.sqrMagnitude or Math.Abs.

1 Like

Make sure you have a problem before you wreck your project with crazy premature optimizations that make your codebase hell to work in. It’s highly unlikely that a camera controller would be a large part of your per-frame work.

Yes.

Camera stuff is pretty tricky… I hear all the Kool Kids are using Cinemachine from the Unity Package Manager.

There’s even a dedicated Camera / Cinemachine area: see left panel.

If you insist on making your own camera controller, do not fiddle with camera rotation.

The simplest way to do it is to think in terms of two Vector3 points in space:

  1. where the camera is LOCATED
  2. what the camera is LOOKING at
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;

void LateUpdate()
{
  cam.transform.position = WhereMyCameraIsLocated;
  cam.transform.LookAt( WhatMyCameraIsLookingAt);
}

Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations. As long as you move those positions smoothly, the camera will be nice and smooth as well, both positionally and rotationally.

1 Like

I don’t think I’m following the last things you said about rotation.

I have a player that moves with mouse, and the script is supposed to tilt the camera when the player moves. So the mouse movement changes the camera based on x and y movement, and z changes it’s rotation as if you tilted your head sideways. LookAt would change all 3 of those points to the Vector3 inputed as parameter, wouldn’t it? That means that I can’t move my camera with my mouse.

Basically this script only changes the z value and doesn’t affect the x and y.

It works perfectly, I was just wondering if the code can be written in a better way somehow.

What is the problem in my changing of the z value?

Can you explain what you meant a little more, I don’t understand how LookAt would be used to do the head tilt effect.

Thank you!

As for Time.deltaTime - A general rule is that if you’re animating something by adding a value to another value from within Update then you’ll need to use Time.deltaTime.

So your tilting code needs Time.deltaTime:

currentRotation += new Vector3(0, 0, camTiltSpeed) * Time.deltaTime;

If you’re ever in doubt and you need to test to see if your game is frame rate independent then just limit the frame rate using Application.targetFrameRate or disable VSync in the game view and test to see if everything behaves the same.

1 Like

Ahhh nice. I had it at first and I thought it was pretty much a random small value, but I supposed it must be something like that time from the last frame?

Instead of having camTiltSpeed be very low (and it being added/reduced each frame), I made it a bigger number and multiplied it with Time.deltaTime and now it is frame-rate independant!

That was very simple and worked instantly thank you!

If you’re using the old input system then you can replace your tilting code with a single line:

transform.localEulerAngles=new Vector3(0,0,-Input.GetAxis("Horizontal") * 10);

Should there be a Time.deltaTime here? This didn’t really work normally when I run it.

Thank you btw this is excatly the type of changes I want, I like compact code :slight_smile:

There’s no need for Time.deltaTime with GetAxis in this case because we’re not adding it to localEulerAngles. But even if using GetAxis to read and add the mouse values we still wouldn’t use Time.deltaTime because the mouse delta values are already frame rate independent.

Me too!

1 Like

I replaced this line:

currentRotation += new Vector3(0, 0, -camTiltSpeed * Time.deltaTime);
transform.localRotation = Quaternion.Euler(currentRotation);

With this:

transform.localEulerAngles=new Vector3(transform.localRotation.eulerAngles.x,
                    transform.localRotation.eulerAngles.y, -PlayerMovement.xMovement * camTiltSpeed);

Now, the camTiltSpeed is working as a maxCamTiltAngle, which is fine I can just replace them but how would I change the tiltSpeed then?

If you’re using GetAxis for xMovement then you can change the sensitivity in the Input Manager. Doing this will let you specify how quickly it tilts (sensivitiy) and how quickly it returns back to a neutral position (gravity).

1 Like