Playmaker global variables to C# script

I’m just learning to use Playmaker and C#. I found an auto-targeting script which has you set the Camera and then the Target. I want to set the m_target to a Global Variable (ClosestTarget_glo) which I set up in a Playmaker FSM with arraymaker to get the closest object with an enemy tag.

How would I change the m_target to the Global Variable ‘ClosestTarget_glo’ …OR assign it to the m_target?

I posted this on the Playmaker forum but also wanted to ask here.

Here’s the Script:

using System.Collections;
using Cinemachine;
using Cinemachine.Utility;
using UnityEngine;

public class SnapToTarget : MonoBehaviour
{
public Transform m_target;
public CinemachineFreeLook m_freeLookCamera;
private bool m_rotating;

// Update is called once per frame
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(LookAtTarget(GetFreeLookYRange()));
}
}

///


/// Call this to cancel the rotation, e.g. if the user rotates the FreeLook
///

public void CancelRotation()
{
m_rotating = false;
}

///


/// This is supposed to snap (or animate) the camera axis to look at the target
///

private IEnumerator LookAtTarget(float yRange)
{
// This constant controls the speed of the lerp. 1 is fastest.
const float lerpAmount = 0.1f;
m_rotating = true;
while (m_rotating && m_target != null && m_freeLookCamera != null)
{
// How far away from center is the target?
var state = m_freeLookCamera.State;
var dir = m_target.position - state.CorrectedPosition;
var rot = state.CorrectedOrientation.GetCameraRotationToTarget(dir, state.ReferenceUp);

// Record the current settings
var current = new Vector2(m_freeLookCamera.m_YAxis.Value, m_freeLookCamera.m_XAxis.Value);

// First do the yaw
m_freeLookCamera.m_XAxis.Value = (current.y + rot.y * lerpAmount) % 360;

// Then do the pitch
m_freeLookCamera.m_YAxis.Value = Mathf.Clamp01(current.x + lerpAmount * rot.x / yRange);

// Are we there yet?
if (Mathf.Abs(m_freeLookCamera.m_YAxis.Value - current.x) < 0.01f
&& Mathf.Abs(m_freeLookCamera.m_XAxis.Value - current.y) < 0.01f)
{
m_rotating = false; // yes, close enough
}
yield return null;
}
}

///


/// Deduces the Y axis angle range from the freeLook orbits
///

private float GetFreeLookYRange()
{
if (m_freeLookCamera == null)
return 1;
var orbit = m_freeLookCamera.m_Orbits[2];
var d0 = new Vector3(0, orbit.m_Height, orbit.m_Radius);
orbit = m_freeLookCamera.m_Orbits[0];
var d1 = new Vector3(0, orbit.m_Height, orbit.m_Radius);
return Vector3.Angle(d0, d1);
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

using System.Collections;
using Cinemachine;
using Cinemachine.Utility;
using UnityEngine;

public class SnapToTarget : MonoBehaviour
{
public Transform m_target;
public CinemachineFreeLook m_freeLookCamera;
private bool m_rotating;

// Update is called once per frame
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(LookAtTarget(GetFreeLookYRange()));
}
}

/// <summary>
/// Call this to cancel the rotation, e.g. if the user rotates the FreeLook
/// </summary>
public void CancelRotation()
{
m_rotating = false;
}

/// <summary>
/// This is supposed to snap (or animate) the camera axis to look at the target
/// </summary>
private IEnumerator LookAtTarget(float yRange)
{
// This constant controls the speed of the lerp. 1 is fastest.
const float lerpAmount = 0.1f;
m_rotating = true;
while (m_rotating && m_target != null && m_freeLookCamera != null)
{
// How far away from center is the target?
var state = m_freeLookCamera.State;
var dir = m_target.position - state.CorrectedPosition;
var rot = state.CorrectedOrientation.GetCameraRotationToTarget(dir, state.ReferenceUp);

// Record the current settings
var current = new Vector2(m_freeLookCamera.m_YAxis.Value, m_freeLookCamera.m_XAxis.Value);

// First do the yaw
m_freeLookCamera.m_XAxis.Value = (current.y + rot.y * lerpAmount) % 360;

// Then do the pitch
m_freeLookCamera.m_YAxis.Value = Mathf.Clamp01(current.x + lerpAmount * rot.x / yRange);

// Are we there yet?
if (Mathf.Abs(m_freeLookCamera.m_YAxis.Value - current.x) < 0.01f
&& Mathf.Abs(m_freeLookCamera.m_XAxis.Value - current.y) < 0.01f)
{
m_rotating = false; // yes, close enough
}
yield return null;
}
}

/// <summary>
/// Deduces the Y axis angle range from the freeLook orbits
/// </summary>
private float GetFreeLookYRange()
{
if (m_freeLookCamera == null)
return 1;
var orbit = m_freeLookCamera.m_Orbits[2];
var d0 = new Vector3(0, orbit.m_Height, orbit.m_Radius);
orbit = m_freeLookCamera.m_Orbits[0];
var d1 = new Vector3(0, orbit.m_Height, orbit.m_Radius);
return Vector3.Angle(d0, d1);
}
}

I’m trying to set a global variable as a target object, either through scripting or Playmaker.

using System.Collections;
using Cinemachine;
using Cinemachine.Utility;
using UnityEngine;
using HutongGames.Playmaker;

public class SnapToTarget : MonoBehaviour
{
public PlaymakerFSM ClosestTarget_glo;
public Transform m_target;
public CinemachineFreeLook m_freeLookCamera;
private bool m_rotating;

// Update is called once per frame
private void Update()
{
fsm.ClosestTarget_glo = m_target;
if (Input.GetMouseButtonDown(0))
{
StartCoroutine(LookAtTarget(GetFreeLookYRange()));
}
}

Doesn’t work so I’m thinking of making an empty object in my scene that has a Smooth Follow Action assigned to closest object in the array.

Any help would be appreciated.