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 follows sphere while looking at a square

Discussion in 'Cinemachine' started by CoreApp2, Sep 27, 2018.

  1. CoreApp2

    CoreApp2

    Joined:
    Sep 8, 2015
    Posts:
    3
    Hello lovely people,

    I try since some few days to give the camera a behavior with the cinemachine tool, but it didnt work. I will try to describe the motion of the camera in the next lines:
    We have a square drawn on the ground and a sphere as the player. the camera should always be behind the sphere, that means that we will always have the alignment camera->sphere->square. The camera follows the sphere from behind, while looking at the square.
    Lets say we throw the sphere. The sphere rolls, the camera follows; the sphere keeps on rolling and passes through the square on the ground. Since the camera looks at the square and follows the sphere from behind, it would rotate around the square to keep follow the sphere from behind while looking at the square (camera->sphere->square). The camera can't pass through the square or enter in the square. It follows the sphere from behind and out of the square.
    What i have done. I defined a virtual cam, set the properties as following

    Follow: Sphere
    Look At: Square
    Body: Transposer
    Aim: Composer

    and adjust the other properties such that the cam can look in the correct direction with the alignment camera->sphere->square
    But there are many problems that occur. First, the motion is not smoothed, the camera moves as it was a child object of the sphere although I increased the damping. Second, the camera still enter in the square or passes through the square. I mean when the camera looks at the square and the field of view is set such that it must see the whole square, it would not follow the sphere and enter in the square, but try to look at the square from outside while keeping following the sphere or not ?
    Can you please help me to achieve this behavior.?

    Thanks
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    What if you tried this:
    Put an invisible child object on the sphere, with a little script that always aligns it so that its forward (the invisible child's) points to the square. Then, use that as the follow target instead of the sphere, putting the transposer in LockToTarget or LockToTargetWithWorldUp binding mode. Set the offset to be (0, 0, -something). That will force the camera-sphere-square arrangement.
     
  3. CoreApp2

    CoreApp2

    Joined:
    Sep 8, 2015
    Posts:
    3
    Hi Gregroryl,
    thanks for the help. Since I had not been so much into the rotation, it had taken a while. I solved it that way.
    The codes for the child are.

    Code (CSharp):
    1.  
    2. void Update () {
    3.         targetRotation = Quaternion.LookRotation(target.position - transform.position);
    4.         transform.rotation = targetRotation;
    5.        
    6.         //// OR    
    7.    
    8.         Vector3 targetPos = new Vector3(target.position.x, transform.position.y, target.position.z);
    9.         transform.LookAt(targetPos);
    10.     }

    With the Quaternion.lookRotation, the curve formed by the child object looks like a bow, and with lookat it is not the case.

    Then I set the parameters of cinemachine as follows
    Follow: Child of sphere
    Look At: Square
    Body: Transposer
    Aim: Composer

    Both LockToTarget or LockToTargetWithWorldUp work. i couldnt see a big difference in the motion of the cam.

    I have one last question. When I throw the sphere. How can I make it that the camera follows the sphere in a defined area? For example, I want the camera to be no higher than y=40 and not less than y=10.
    If I throw the sphere and it goes over y = 40, then the camera stays at y = 40 (keeps folow on x, z , y=40) and does not follow it above the 40, but when the sphere goes down again, meaning y <= 40, then the camera continues to follow the sphere.

    Thanks
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,658
    You can use a CinemachineConfiner do define a region where the camera is allowed to go. It will be stopped at the edges of this region.