Making a character move using visual scripting in Unity

Let’s try out a simple experiment. Let’s take the movement script from the tutorial on how to move an object in Unity and try the same with Unity’s visual scripting system.

If you are using Unity 2021 or later you need not install any additional package to use visual scripting.

Here is the code to move an object

using UnityEngine;
public class move_player : MonoBehaviour
{
    Vector3 pos;

    void Start()
    {
        pos=transform.position;
    }
    // Update is called once per frame
    void Update()
    {
       float x_move=Input.GetAxis("Horizontal");
       float z_move=Input.GetAxis("Vertical"); 
       pos.x+=x_move*Time.deltaTime;
       pos.z+=z_move*Time.deltaTime;
       transform.position=pos;
        
    }
}

Here is the same in visual scripting

Free Unity asset banner

Let’s see how to do it

Adding a flow graph

  1. Select your object in the hierarchy window.
  2. Go to inspector and click on Add Component.
  3. Select Script Machine from the options.
  4. This will add two components to your object. Variables and Script Machine.
  5. Select source as graph and create a new graph.
  6. Click edit graph.

Creating the logic

  1. We don’t need the start event. You can remove that.
  2. Add an input get axis block and connect it to the update event.
  3. Add the axis name as “Vertical”. Duplicate and create another input block and type in the axis name as ” Horizontal”.
  4. Create two multiplier blocks and connect the output of each input get axis block to multiplier first input.
  5. Add get delta time block as the second input to both the multipliers.
  6. Create a vector 3 and connect the multiplier output to x and z axis values.
  7. Use transform get position to get the position of the object.
  8. Add the vector 3 with position and set it to the object position using set transform block.

Leave a Reply

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