Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Need help deciphering my own damn code (Camera movement)

Discussion in 'Scripting' started by avitohol, Jul 26, 2023.

  1. avitohol

    avitohol

    Joined:
    May 4, 2021
    Posts:
    6
    Hey y'all. So a while back I wrote a method that could handle camera movement properly along an isometric plane (as in, moving the camera up moves it forward the way you think it would, not along the actual xyz axes). I remember it took me like a whole day of frankensteining tips from other people online to figure out how to do it without the y axis moving. It's working fine and I should never ever touch it again, but I noticed today that when I drag the mouse up (i.e. when I move the camera forward) I noticed it's a little less sensitive than when I drag left or right. Problem is I don't know where in the code I could put a yDragSensitivityvariable to fix it.

    Here's the method:

    Code (CSharp):
    1. void Drag() {
    2.         if (Input.GetMouseButton(1)) // right mouse button for drag
    3.         {
    4.             // calculate the movement vector based on camera orientation, how did I come up with this?
    5.             Vector3 forwardDirection = transform.forward;
    6.             Vector3 rightDirection = Quaternion.Euler(0, 90, 0) * forwardDirection; // literally why did I do this? I mean, it's working ...
    7.  
    8.             Vector3 movement = (rightDirection * -mouseX + forwardDirection * -mouseY) * dragSpeed * Time.deltaTime;
    9.             movement.y = 0f; // set the movement in y axis to 0 to keep the camera height fixed
    10.  
    11.             // apply movement to the camera
    12.             // transform.position += movement;
    13.             Vector3 targetPosition = transform.position + movement;
    14.             transform.position = Vector3.Lerp(transform.position, targetPosition, 0.5f);
    15.         }
    16.     }
    17. }
    Now, I'll admit it's really not that noticeable, but as a perfectionist if I don't fix this my soul will be tormented for all eternity.

    Thanks in advance guys.
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    can't you just multiply this part with
    It feels like you described your own problem and the solution to it o_O
     
    avitohol likes this.
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Instead of this, why not just use transform.right?

    when you set the movement in this line:
    Code (csharp):
    1.  
    2. Vector3 movement = (rightDirection * -mouseX + forwardDirection * -mouseY) * dragSpeed * Time.deltaTime;
    3.  
    Just multiply the "forwardDirection" by a number (greater than 1, obviously). If you want to make it "variable", go ahead. It's good practice if you name it descriptively.
     
    avitohol and venediklee like this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Let me up the game and say, "Instead of this, why not just use Cinemachine?" :)

    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    Otherwise, if you insist on doing it yourself, when I read this:

    The first thing i think is perhaps you are scaling the camera movement to the raw mouse movement and not taking into account camera angle? If your camera is isometric, this is exactly the result you would get, as one axis is effectively scaled to screen coordinates at a different rate than the other axis, based on the angle of viewing.
     
    avitohol and venediklee like this.
  5. Sluggy

    Sluggy

    Joined:
    Nov 27, 2012
    Posts:
    839
    Technically in a 3D space this would be accurate but what you are noticing is due to the fact that the horizontal movement is more-or-less 1:1 with the distance in space on the screen but when moving the mouse vertically you are traveling through the depth of the space so foreshortening causes less apparent vertical movement. I had a similar issue with one of my own games where I used a weird perspective camera to make things appear to be a 2D scene that you'd typically see in older games that were faking a 3D view using isometric graphics (sounds weird I know. I have a tendency to makes technology in my games that mimcs old fashioned techniques that were used due to limits of hardware. But I end up having to fake them because I'm actually using 3D lol)


    Anyway, I'd probably just multiply the scaler it with the mouseY value you assign to the 'movement' vector.

    So for example
    rightDirection * -mouseX + forwardDirection * -mouseY)

    Becomes
    rightDirection * -mouseX + forwardDirection * -mouseY * yDragSensitivityvariable)
     
    avitohol likes this.
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Also, a good idea. It might be overkill in this case, but now I use Cinemachine in every Unity project I start. It takes a little time to learn what all of the settings do, though,
     
    avitohol and Kurt-Dekker like this.
  7. avitohol

    avitohol

    Joined:
    May 4, 2021
    Posts:
    6
    This is why I love this community — look at all these great answers. You're real OGs for pulling through.

    It was right in front of me and I didn't notice it. Thanks, guys, that was exactly it. I guess the rightDirection = Quaternion.Euler(0, 90, 0) * forwardDirection line was throwing me off, I think the reason why I used that and not transform.right was because since the camera rotates, I thought transform.right wouldn't work properly. It does. That one line made my BrainController.cs question all the other lines.

    ...
    Don't laugh at me, but I completely forgot Cinemachine exists :rolleyes:. My current messy CameraController.cs works (for now), so I won't mess with it, but I'll be sure to remember that Cinemachine is a thing next time.

    Anyway, thank you so much guys! Be sure to be on the lookout for my next dumb question.
     
    Kurt-Dekker likes this.