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 Making the Camera rotate around the player based on it's velocity.

Discussion in 'Scripting' started by tjocampbell03, Sep 7, 2023.

  1. tjocampbell03

    tjocampbell03

    Joined:
    Sep 7, 2023
    Posts:
    2
    I'm working on a third person game where you follow a marble controlled by physics/rigidbody properties. My camera can follow the marble, but I'd like it to rotate around it based on the velocity vector of the ball. Basically, I want the camera to look in the direction the player is moving, but I haven't gotten anywhere close. Does anyone have any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    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/

    If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.

    Code (csharp):
    1. private Vector3 WhereMyCameraIsLocated;
    2. private Vector3 WhatMyCameraIsLookingAt;
    3.  
    4. void LateUpdate()
    5. {
    6.   cam.transform.position = WhereMyCameraIsLocated;
    7.   cam.transform.LookAt( WhatMyCameraIsLookingAt);
    8. }
    Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.

    In your "look-in-ball-moving direction" game question specifically, you could flatten the movement vector (set its y to zero) and as long as it is above a certain magnitude, then drive the "look from" and "look at" values above.

    Third person camera viewing, relative controls, gradual turning, and movement separated from camera:

    https://forum.unity.com/threads/third-person-camera-movement-script.858673/#post-5960519
     
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    True ^..

    One trick I did, when using one camera, but having multiple places it could be at any given time, was have a childed object to said thing to follow. Basically a camera position you can see, while modifying it's values in Editor. Then just like Kurt mentioned, tell camera to be at that position, and then look in the direction you want.

    Not exactly sure how you have the marble setup, because if it rolls entirely, you can't use it's forward(or use a child for position). But velocity should determine the look Vector, as that would be the world space position it's going to anyway. But with a marble hitting things, and bouncing all over, I can see that camera giving you motion sickness pretty quick, lol.

    So you'd need a bit of math to smooth out all those indifferences, but again, like Kurt mentioned, Cinemachine I think handles all of that for you. Not sure, haven't played around with it yet.
     
  4. tjocampbell03

    tjocampbell03

    Joined:
    Sep 7, 2023
    Posts:
    2
    This is great, thanks a lot for responding. I'll try it out when I get home and let you know how it goes.
     
  5. faUnity

    faUnity

    Joined:
    Aug 7, 2023
    Posts:
    13