Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Camera always behind player | Camera rotating towards players' movement direction

Discussion in 'Scripting' started by Feuerputz, Jun 25, 2019.

  1. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    Hey folks,
    I´ve been searching everywhere for a solution, but couldn't find useful things.
    As the title suggests, I want my camera to rotate automatically to stay in the back of my player. (Like temple run for instance).
    It´s a roll-a-ball game where I add force to the rigidbody of the ball. Currently the camera is following the player without any problems.

    Thanks for any hints and ideas! :)
     
  2. arahmitz

    arahmitz

    Joined:
    Jun 25, 2019
    Posts:
    25
    Can you give any quick video on how it works now? The "easiest" way is to make a empty object called Player, and child things like model, camera, etc.
     
  3. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    of course.

    This shows the movement of the ball, als well as the scripts "Camera_Follow" and "Ball_Base".
     
  4. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    just posted a comment with a video here.
    Thanks for your answer!
    I had the same idea, but parenting the camera to the player leads to the problem, that the cam will rotate with the ball. And adding both as childs to the same GameObject (Empty) also doesn´t work.
     
  5. arahmitz

    arahmitz

    Joined:
    Jun 25, 2019
    Posts:
    25
    Hm, I don't understand what you need then. If you child the camera into the model it inherits the change of position. So lets say we are on [0,0,0] and the camera is on [2,2,2], then if we go in like [0,1,0] so our new position becomes [0,1,0], the camera will go into [2,3,2] too. When you child both model and camera into object, you want to move whole "empty" gameobject, not the player that is now a child.
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    1) get a vector that is the movement delta, something to act as transform.forward in this case where you roll on all axis and transform.forward wont work, maybe rigidbody.velocity.normalized?

    2) set the camera position to this vector multiplied by negative distance, negative so you move the camera back and not forward.

    3) rotate the camera to look at the player in your preferred way.

    You may want to limit some aspect of the vector, the Y axis perhaps off the top of my head.
     
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Yeah, but he's a ball that rotates, the camera is gonna start orbiting around.
     
    Feuerputz likes this.
  8. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    I only want the camera to stay always in the opposit direction of movement. if the player press "D" and the ball rolls right then, the camera should rotate left (looking right.
     
  9. arahmitz

    arahmitz

    Joined:
    Jun 25, 2019
    Posts:
    25
    What if he would use addforce rather than transform? I believe I tried it a long time ago, but I won't hold to my truth, and I cant check it for now.

    Use SparrowsNest algorithm then.
     
  10. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43

    1) I tried this:
    private void OnDrawGizmos()
    {
    Gizmos.color = Color.green;
    Rigidbody rb = this.GetComponent<Rigidbody>();
    Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
    Gizmos.DrawRay(transform.position, localVelocity * 33f);
    }
    what would rb.velocity.normalized do?
     
  11. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43

    Now the Gizmo is rotating (in the oppsite) rolling direction. --> using transform.InverseTransformVector instead of InversetransformDirection. Meaning when I roll forward, the gizmo is rotating backwards.

    Then I´ve locked the rotation constrains of my balls' rigidbody, but it works better, but not perfect... I´ll upload a video. Give me a minute
     
  12. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43


    Moving the ball with A,S,W,D. The Gizmo reacts on the movement direction. Moving right and left seems to work. The Gizmo shows the moving direction, but forward and backwards as well as multiple key-inputs doesnt work properly.
    Code:

    private void OnDrawGizmos()
    {
    Gizmos.color = Color.green;
    Rigidbody rb = this.GetComponent<Rigidbody>();
    Vector3 localVelocity = transform.InverseTransformVector(rb.velocity.normalized);
    Gizmos.DrawRay(transform.localPosition, localVelocity * 33f);
    }
     
  13. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    Ok guys, I solved the problem with the Gizmo!
    I made it way to complicated. Just this: no need for InverseTransformVector...
    private void OnDrawGizmos()
    {
    Gizmos.color = Color.green;
    Rigidbody rb = this.GetComponent<Rigidbody>();
    Vector3 localVelocity = rb.velocity.normalized;
    Gizmos.DrawRay(transform.localPosition, localVelocity * 8f);
    }

    How might I rotate the camera with this vector? That´s the only thing that´s unclear to me.

    Thank you all for helping!
     
  14. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You didn't fully follow step 2 and skipped step 3.

    edit: i also dont understand why you tried moving the vector to local space.

    @arahmitz I pretty sure the rigidbody is gonna rotate when moving on terrain.
     
  15. Feuerputz

    Feuerputz

    Joined:
    Sep 6, 2017
    Posts:
    43
    I remember step 2. That´s not the problem, but I dont know how to get the rotation of the camera from the vector.