Udon Basics
This page contains examples of how to use Udon. All examples can be viewed as an Udon Graph or in UdonSharp.
Rotating cube
This behaviour rotates a game object (such as a cube) by 90 degrees every second on its local Y-axis.
- Udon Graph
- UdonSharp
using UnityEngine;
using VRC.SDKBase;
public class RotatingCubeBehaviour : UdonSharpBehaviour
{
private void Update()
{
transform.Rotate(Vector3.up, 90f * Time.deltaTime);
}
}
Interact
This behaviour uses Interact to allow players interact with a object to disable it. This can be used for a message or a door that disappears when players click on it, for example. The game object must have a collider component to allow players to interact with it.
- Udon Graph
- UdonSharp
using UnityEngine;
using VRC.SDKBase;
public class ClickMe: UdonSharpBehaviour
{
public override void Interact()
{
gameObject.SetActive(false);
}
}
Teleport player
This behaviour uses Interact and TeleportTo to teleport the player. The targetPositon
transform determines the player's destination position and rotation after teleporting. Don't forget to add a collider component to the targetPosition GameObject.
- Udon Graph
- UdonSharp
using UnityEngine;
using VRC.SDKBase;
public class TeleportPlayer : UdonSharpBehaviour
{
public Transform targetPosition;
public override void Interact()
{
Networking.LocalPlayer.TeleportTo(
targetPosition.position,
targetPosition.rotation);
}
}
Sending events
This behaviour shows how to interact with other behaviours. UdonBehaviours can communicate with each other through variables and custom events.
- Udon Graph
- UdonSharp
using UdonSharp;
using UnityEngine;
using VRC.Udon.Common.Interfaces;
public class SomeExample : UdonSharpBehaviour
{
[SerializeField] private SomeOtherExample otherBehaviour;
void Start()
{
if(otherBehaviour.somePublicBoolean)
{
otherBehaviour.SomeCustomEvent();
}
}
public override void Interact()
{
DoStuff();
}
private void DoStuff()
{
SendCustomNetworkEvent(NetworkEventTarget.All, nameof(DoNetworkEventStuff));
}
public void DoNetworkEventStuff()
{
otherBehaviour.somePublicBoolean = false;
otherBehaviour.SomeCustomEvent();
otherBehaviour.SendCustomNetworkEvent(NetworkEventTarget.Owner, nameof(DoOwnerStuff));
}
}