Making camera follow player using visual scripting in Unity

In this tutorial, we will see how to make a camera follow player using visual scripting. You can also check out other camera follow methods in our camera follow player using script tutorial

I am using Unity 2021.1.13 for this tutorial

I have a cube in my scene with the movement control attached to it. Let’s make the camera follow the cube when it moves using visual scripting. Unity 2021 has visual scripting already installed with the package.

Create and add a flow graph

  1. Select the camera.
  2. Go to inspector window and click on Add Component.
  3. Select and add Script Machine.
  4. Click on New to create a new graph.
  5. Let’s name it Camera_follow.

Creating the logic

Creating variables

  1. We need two variables for this process.
  2. First one is a gameobject which will be our player.
  3. The second one is a float that will have the ofset value of the camera’s z-axis.
object variables

Editing the flow graph

  1. Click on edit graph in the inspector window.
  2. Add set variable block near the start event.
  3. Add a get position block and a get z block.
  4. Take the z value from the transform and assign it to the set variable block.
  5. Select the variable as z offset and connect the start event to the set variable block.
visual scripting nodes in the start event
  1. In the update event add get variable, get position, get z, get x, add and set position block.
  2. Set the get variable block to type object and select player as the variable.
  3. Connect output of get variable to the get transform block. This way we can get the position of the player.
  4. Get the x and z position from the get position block. Add the z value with the float variable from start event.
  5. Get y from the get position of camera in the start event.
  6. Create a new vector 3 and assign x,y and z from the Get X, Get Y and Get Z blocks.
  7. Connect the vector3 to set position of the camera
visual scripting nodes in the update event

Final output

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.