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
- Select the camera.
- Go to inspector window and click on Add Component.
- Select and add Script Machine.
- Click on New to create a new graph.
- Let’s name it Camera_follow.
Creating the logic
- We need two variables for this process.
- First one is a gameobject which will be our player.
- The second one is a float that will have the offset value of the camera’s z-axis.
Editing the flow graph
- Click on edit graph in the inspector window.
- Add set variable block near the start event.
- Add a get position block and a get z block for the Camera and one for the Player.
- Take the z value from the transform and subtract the Camera z position with the Player z position.
- Select the difference to z offset and connect the start event to the set variable block.
- In the Lateupdate event add get variable, get position, get z, get x, add and set position block.
- Set the get variable block to type object and select player as the variable.
- Connect output of get variable to the get transform block. This way we can get the position of the player.
- Get the x and z position from the get position block of the Player transform. Add the z value with the float variable from start event.
- Get y from the get position of camera in the start event.
- Create a new vector 3 and assign x, y and z from the Get X, Get Y and Get Z blocks.
- Connect the vector3 to set position of the camera