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.

Third-Person Camera Orbit issues...

Discussion in 'Scripting' started by WhipJr, Sep 15, 2021.

  1. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    Hello!

    I currently have a Camera Follow that's pretty simple, however I am having an issue where when I initiate the controls to rotate the camera, if the player is moving, then the camera watches as the player moves away from it. It seems as though it's related to the z axis of my offset, but I have tooled around with that to no avail.

    Below is the code that rotates my camera around the player.
    Code (CSharp):
    1. void LateUpdate()
    2.     {
    3.         if (UpdateCamera)
    4.             UpdateCameraPosition(changeDelta);
    5.         else
    6.             UpdateCameraPosition();
    7.     }
    8.     void UpdateCameraPosition()
    9.     {
    10.         transform.position = Vector3.Lerp(transform.position, target.position - offset, smoothSpeed);
    11.         UpdateCameraRotation();
    12.     }
    13.     private void UpdateCameraPosition(Vector2 delta)
    14.     {
    15.         transform.position = Vector3.Lerp(transform.position, transform.position + (-transform.right * delta.x * 4), smoothSpeed);
    16.         transform.position = Vector3.Lerp(transform.position, transform.position + (-transform.up * delta.y * 4), smoothSpeed);
    17.  
    18.         UpdateCameraRotation();
    19.  
    20.         offset = target.position - transform.position;
    21.  
    22.         UpdateCamera = false;
    23.         changeDelta = Vector2.zero;
    24.     }
    25.     void UpdateCameraRotation()
    26.     {
    27.         transform.LookAt(target);
    28.     }
    29.     public void MoveCamera(Vector2 delta)
    30.     {
    31.         UpdateCamera = true;
    32.         changeDelta = delta;
    33.     }
    When holding right-click, UpdateCameraPosition is called with the Mouse X and Mouse Y Axis as the Vector 2
    delta. otherwise, UpdateCameraPosition is called.


    This video shows the issue I am having. Everything up until about 13 seconds shows things working. after that, I begin moving the player while holding the right-click and you can see the exact issue I am talking about.

    Any advice or suggestions on this would be greatly appreciated.

    Thank you!
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,584
    Is there any particular reason you're doing it this way? Why not make the camera a child of the empty and have the empty follow the position of the player? This seems to be the most tried and true method for me when it comes to third person or even top down cameras.
     
  3. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    Not particularly, other than I'm teaching students programming in Unity and I'm trying to keep as much as i can in code so they have more to look back on when doing future projects.
     
  4. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,584
    You're teaching them bad practices in this case, the whole point of game engines is that they make games development easier and more efficient. There's also a pretty easy argument to be made in this case that compare to simply making good use of the parenting system you're writing lots of unnecessary code and that's wasting time and computing power that can be put to other tasks.

    If you want to teach them purely about code, then either teach them how to code games entirely from scratch or teach them code by itself and how to apply it to these situations correctly. If you don't believe me, check out the unity profiler and compare the two methods. In the case of this method versus the parenting method they could end up exactly the same and I don't discount that. In which case though your students would just be wasting time writing unnecessary code when they could setup a perfectly acceptable third person camera in a few minutes.
     
    WhipJr likes this.
  5. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    You may be right, but as i said, its to expose them to as much as possible for later on. showing them the easier way isn't the priority at this point. There's not a "wrong way" to make a camera follow script, yes you could do it faster, and easier, but that isn't the goal with this. If i wanted to, I could delve into CineMachine and use the 3rd person follow from that, but again, easier is not the point here, but exposure.

    Maybe you're right here, but either way I'm looking for a coding solution to the problem which hopefully someone can chime in on.

    Edit: I did do the parenting thing as having something that works is the priority overall for the students, however I would still like to know if anyone has a solution for the topic issue as It's still bothering me that I couldn't see how to fix what was wrong.
     
    Last edited: Sep 16, 2021
  6. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    I suspect that something is setting UpdateCamera to true each frame.
     
  7. WhipJr

    WhipJr

    Joined:
    Aug 3, 2011
    Posts:
    125
    Even if I got rid of the else statement in FixedUpdate the problem still existed (that's how I had it originally). Update camera is only set when the right-click was held which works is as intended. Just not the moving away from the camera part.