Calling Unity and VRChat types
You call Unity and VRChat types the same way you would in C# (for getting components, see Getting and calling components). Only members exposed to Udon can be called; a member that is not exposed is an error at compile time (Forms that don’t compile).
Forms that compile (14)
Section titled “Forms that compile (14)”| Syntax | Description | Note |
|---|---|---|
source.Play() | Playing a sound | |
source.PlayOneShot(clip, 0.7f) | Layering a sound effect | |
animator.SetTrigger("Open") | Switching an animation with a trigger | |
animator.SetBool("IsOpen", open) | Switching an animation with a bool | |
body.AddForce(force, ForceMode.Impulse) | Applying a force | |
Physics.Raycast(origin, direction, out hit, 12.5f) | Checking for a hit with a ray | |
transform.Translate(velocity * Time.deltaTime) | Moving every frame | |
transform.SetPositionAndRotation(position, rotation) | Setting position and rotation | |
door.SetActive(!door.activeSelf) | Showing and hiding | |
Instantiate(prefab) | Copying a prefab | Set the position and rotation after copying (you cannot pass them as arguments). |
Input.GetKeyDown(KeyCode.Space) | Reading a key | VR controller input is received through the input events on the Events page. |
label.text = "count: " + count | Showing text | |
particles.Emit(12) | Emitting particles | |
pickup.Drop() | Dropping a held object |
Forms that don’t compile (4)
Section titled “Forms that don’t compile (4)”| Syntax | Description | Error | Reason | Alternative |
|---|---|---|---|---|
gameObject.AddComponent<Rigidbody>() | Adding a component at run time | CS1061 | runtime | Place an object that already has the component and show it with SetActive |
target.SendMessage("Open") | Calling a method by name through Unity’s messaging | CS1061 | runtime | Call it with SendCustomEvent |
Resources.Load("Door") | Loading an asset by name | CS0103 | runtime | Pass a reference in the Inspector |
Instantiate(prefab, position, rotation) | Copying with the position and rotation as arguments | CS1501 | runtime | Call SetPositionAndRotation after Instantiate(prefab) |
Sound and animation
Section titled “Sound and animation”Playing a sound
Section titled “Playing a sound”Pass an AudioSource reference in the Inspector and call Play.
using UnityEngine;using Tsukimi;
public class UnityTypesAudioPlay : TsukimiBehaviour{ public AudioSource source;
public override void Interact() { source.Play(); }}Layering a sound effect
Section titled “Layering a sound effect”Pass a clip and a volume scale to PlayOneShot.
using UnityEngine;using Tsukimi;
public class UnityTypesAudioOneShot : TsukimiBehaviour{ public AudioSource source; public AudioClip clip;
public override void Interact() { source.PlayOneShot(clip, 0.7f); }}Switching an animation with a trigger
Section titled “Switching an animation with a trigger”Name the Animator parameter.
using UnityEngine;using Tsukimi;
public class UnityTypesAnimatorTrigger : TsukimiBehaviour{ public Animator animator;
public override void Interact() { animator.SetTrigger("Open"); }}Switching an animation with a bool
Section titled “Switching an animation with a bool”Change a bool parameter with SetBool.
using UnityEngine;using Tsukimi;
public class UnityTypesAnimatorBool : TsukimiBehaviour{ public Animator animator; private bool open;
public override void Interact() { open = !open; animator.SetBool("IsOpen", open); }}Physics and position
Section titled “Physics and position”Applying a force
Section titled “Applying a force”Call it on a Rigidbody reference.
using UnityEngine;using Tsukimi;
public class UnityTypesRigidbodyForce : TsukimiBehaviour{ public Rigidbody body; public Vector3 force = new Vector3(0f, 4.5f, 1.25f);
public override void Interact() { body.AddForce(force, ForceMode.Impulse); }}Checking for a hit with a ray
Section titled “Checking for a hit with a ray”Arguments received with out are written the same way as in C#.
using UnityEngine;using Tsukimi;
public class UnityTypesPhysicsRaycast : TsukimiBehaviour{ public Transform marker;
void Update() { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, 12.5f)) { marker.position = hit.point; } }}Moving every frame
Section titled “Moving every frame”Your own Transform is available as transform.
using UnityEngine;using Tsukimi;
public class UnityTypesTransformMove : TsukimiBehaviour{ public Vector3 velocity = new Vector3(0f, 0f, 1.5f);
void Update() { transform.Translate(velocity * Time.deltaTime); }}Setting position and rotation
Section titled “Setting position and rotation”Build a rotation from angles with Quaternion.Euler.
using UnityEngine;using Tsukimi;
public class UnityTypesTransformPlace : TsukimiBehaviour{ void Start() { transform.SetPositionAndRotation(new Vector3(2f, 0.5f, -3f), Quaternion.Euler(0f, 90f, -15f)); }}Objects and input
Section titled “Objects and input”Showing and hiding
Section titled “Showing and hiding”Call it on a GameObject reference.
using UnityEngine;using Tsukimi;
public class UnityTypesGameObjectActive : TsukimiBehaviour{ public GameObject door;
public override void Interact() { door.SetActive(!door.activeSelf); }}Copying a prefab
Section titled “Copying a prefab”Instantiate can only be written with a single prefab GameObject as its argument.
using UnityEngine;using Tsukimi;
public class UnityTypesInstantiate : TsukimiBehaviour{ public GameObject prefab;
public override void Interact() { GameObject copy = Instantiate(prefab); copy.transform.SetPositionAndRotation(new Vector3(1.5f, 0f, -2f), Quaternion.Euler(0f, 45f, 0f)); }}Reading a key
Section titled “Reading a key”Read keyboard input with Input.
using UnityEngine;using Tsukimi;
public class UnityTypesInputKey : TsukimiBehaviour{ public GameObject door;
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { door.SetActive(!door.activeSelf); } }}Display and VRChat components
Section titled “Display and VRChat components”Showing text
Section titled “Showing text”Assign a string to the text of a TextMeshProUGUI.
using UnityEngine;using Tsukimi;using TMPro;
public class UnityTypesTextSet : TsukimiBehaviour{ public TextMeshProUGUI label; private int count;
public override void Interact() { count++; label.text = "count: " + count; }}Emitting particles
Section titled “Emitting particles”Call it on a ParticleSystem reference.
using UnityEngine;using Tsukimi;
public class UnityTypesParticlesEmit : TsukimiBehaviour{ public ParticleSystem particles;
public override void Interact() { particles.Emit(12); }}Dropping a held object
Section titled “Dropping a held object”VRChat components are called the same way.
using UnityEngine;using Tsukimi;using VRC.SDK3.Components;
public class UnityTypesPickupDrop : TsukimiBehaviour{ public VRCPickup pickup;
public override void Interact() { pickup.Drop(); }}