playMaker

Author Topic: Set Global Variable as Target in Auto-Targeting C# Script  (Read 693 times)

Ningkamess

  • Playmaker Newbie
  • *
  • Posts: 3
Set Global Variable as Target in Auto-Targeting C# Script
« on: October 14, 2021, 03:51:04 PM »
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 an FSM with arraymaker to get the closest object with an enemy tag.


Ningkamess

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Set Global Variable as Target in Auto-Targeting C# Script
« Reply #1 on: October 14, 2021, 03:56:33 PM »
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()));
        }
    }

    /// <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);
    }
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Set Global Variable as Target in Auto-Targeting C# Script
« Reply #2 on: October 15, 2021, 01:42:18 AM »
Hi.
Have a look at the Playmaker API

you can find some samples there on how to communicate with scripts

Ningkamess

  • Playmaker Newbie
  • *
  • Posts: 3
Re: Set Global Variable as Target in Auto-Targeting C# Script
« Reply #3 on: October 15, 2021, 12:33:06 PM »
I looked at the API, it has some good information but I might need a clearer explanation.

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

Code: [Select]
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.