4. Using physics from your scripts
All the scripts in this section go on a GameObject in your scene. They all use the Unity Vector3 you already know, and MultiConnectSDK converts it.
Pushing a body
Script 4.1: PushBody.cs
using MultiConnect.Physics.Core;
using MultiConnect.Physics.Unity;
using UnityEngine;
public class PushBody : MonoBehaviour
{
public MCRigidBodyComponent body;
public void Hit(Vector3 direction, float strength)
{
// One instant push, like a cue hitting a ball.
body.AddImpulse(direction * strength);
}
void FixedUpdate()
{
// A steady push, called every step, like an engine.
body.AddForce(Vector3.forward * 2f, MCForceMode.Force);
}
public void Respawn()
{
body.Teleport(Vector3.up * 2f, Quaternion.identity);
body.linearVelocity = Vector3.zero;
}
}
Knowing when bodies touch
Connect to the body's events in Start, because the body does not exist before then.
Script 4.2: LogCollisions.cs
using MultiConnect.Physics.Unity;
using UnityEngine;
public class LogCollisions : MonoBehaviour
{
public MCRigidBodyComponent body;
void Start()
{
body.Body.OnCollisionEnter += contact => Debug.Log("Touched another body");
body.Body.OnCollisionExit += contact => Debug.Log("Stopped touching");
}
}
Zones
Add GameObject > MultiConnect Physics > Trigger Sphere as a child of the Physics Space, and put Script 4.3 on it.
Script 4.3: PocketZone.cs
using MultiConnect.Physics.Unity;
using UnityEngine;
public class PocketZone : MonoBehaviour
{
void Start()
{
GetComponent<MCTriggerComponent>().OnEnter += body => Debug.Log("Entered: " + body.name);
}
}
Finding what is in the way
Add GameObject > MultiConnect Physics > Raycaster as a child of the Physics Space, and aim it with its Transform. Max Distance sets how far it looks.
Script 4.4: AimCheck.cs
using MultiConnect.Physics.Unity;
using UnityEngine;
public class AimCheck : MonoBehaviour
{
public MCRaycasterComponent raycaster;
void Update()
{
if (raycaster.Cast(out var hit)) Debug.Log("Aiming at: " + hit.Body);
}
}
A character
Add MCCharacterControllerComponent to an object with a rigid body and a capsule collider. It reads the keyboard by itself. To drive it from your own input or from the network, turn off Read Keyboard Input and use Script 4.5.
Script 4.5: DriveCharacter.cs
using MultiConnect.Physics.Unity;
using UnityEngine;
public class DriveCharacter : MonoBehaviour
{
public MCCharacterControllerComponent character;
public void OnInput(Vector3 direction, bool jump)
{
character.SetMoveInput(direction);
if (jump) character.Jump();
}
}

