playMaker

Author Topic: Arena camera - How to do this in Playmaker[SOLVED]  (Read 1817 times)

LogLady

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Arena camera - How to do this in Playmaker[SOLVED]
« on: August 18, 2014, 11:20:48 AM »
Hi!

I'm trying to do a multiplayer arena camera to track 4+ players using one camera and letting all players on screen. Looking around the internet I found this script and would like to know how to do this using Playmaker but I don't have a clue on how to do this.

Code: [Select]
using UnityEngine;

public class TrackTargets : MonoBehaviour {

    [SerializeField]
    Transform[] targets;

    [SerializeField]
    float boundingBoxPadding = 2f;

    [SerializeField]
    float minimumOrthographicSize = 8f;

    [SerializeField]
    float zoomSpeed = 20f;

    Camera camera;

    void Awake ()
    {
        camera = GetComponent<Camera>();
        camera.orthographic = true;
    }

    void LateUpdate()
    {
        Rect boundingBox = CalculateTargetsBoundingBox();
        transform.position = CalculateCameraPosition(boundingBox);
        camera.orthographicSize = CalculateOrthographicSize(boundingBox);
    }

    /// <summary>
    /// Calculates a bounding box that contains all the targets.
    /// </summary>
    /// <returns>A Rect containing all the targets.</returns>
    Rect CalculateTargetsBoundingBox()
    {
        float minX = Mathf.Infinity;
        float maxX = Mathf.NegativeInfinity;
        float minY = Mathf.Infinity;
        float maxY = Mathf.NegativeInfinity;

        foreach (Transform target in targets) {
            Vector3 position = target.position;

            minX = Mathf.Min(minX, position.x);
            minY = Mathf.Min(minY, position.y);
            maxX = Mathf.Max(maxX, position.x);
            maxY = Mathf.Max(maxY, position.y);
        }

        return Rect.MinMaxRect(minX - boundingBoxPadding, maxY + boundingBoxPadding, maxX + boundingBoxPadding, minY - boundingBoxPadding);
    }

    /// <summary>
    /// Calculates a camera position given the a bounding box containing all the targets.
    /// </summary>
    /// <param name="boundingBox">A Rect bounding box containg all targets.</param>
    /// <returns>A Vector3 in the center of the bounding box.</returns>
    Vector3 CalculateCameraPosition(Rect boundingBox)
    {
        Vector2 boundingBoxCenter = boundingBox.center;

        return new Vector3(boundingBoxCenter.x, boundingBoxCenter.y, camera.transform.position.z);
    }

    /// <summary>
    /// Calculates a new orthographic size for the camera based on the target bounding box.
    /// </summary>
    /// <param name="boundingBox">A Rect bounding box containg all targets.</param>
    /// <returns>A float for the orthographic size.</returns>
    float CalculateOrthographicSize(Rect boundingBox)
    {
        float orthographicSize = camera.orthographicSize;
        Vector3 topRight = new Vector3(boundingBox.x + boundingBox.width, boundingBox.y, 0f);
        Vector3 topRightAsViewport = camera.WorldToViewportPoint(topRight);

        if (topRightAsViewport.x >= topRightAsViewport.y)
            orthographicSize = Mathf.Abs(boundingBox.width) / camera.aspect / 2f;
        else
            orthographicSize = Mathf.Abs(boundingBox.height) / 2f;

        return Mathf.Clamp(Mathf.Lerp(camera.orthographicSize, orthographicSize, Time.deltaTime * zoomSpeed), minimumOrthographicSize, Mathf.Infinity);
    }
}


Link to the author: http://nielson.io/2014/03/making-a-target-tracking-orthographic-camera-in-unity/

Can someone build a fsm or translate to analogous functions in Playmaker?

Thanks!
« Last Edit: February 24, 2015, 05:50:13 AM by jeanfabre »

Lane

  • Administrator
  • Hero Member
  • *****
  • Posts: 2484
  • Yup.
    • View Profile
    • Cleverous
Re: Arena camera - How to do this in Playmaker
« Reply #1 on: August 20, 2014, 08:37:27 AM »
Have you checked this out?

http://hutonggames.com/playmakerforum/index.php?topic=6843.msg33445#msg33445

I think thats the closest thing we've done around here on the topic. It's probably possible to convert the script to an action, but it might be easier to just use the script, then access it with the ScriptControl actions.

LogLady

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #2 on: August 22, 2014, 12:01:15 PM »
Nice thing!

It didn't show on the search because I was looking for arena camera / camera multiplayer.

I followed the example but got stuck on putting it to track four players. I added p1 + p2 and saved to a v3 var (midPoint1) then added p3 + p4 and saved to another v3 var (midPoint2) and then using the vector3 operator, added these two v3 vars and saved the result to a 3rd v3 var (midPointTotal). How can I get the distance between 3+ players and put it in a var to set the float clamp, multiply this float and set the camera to smoothly follow the "target" object?

About the script described earlier i'd like to make it using Playmaker to learn how to "translate" it so to know that when a script asks for translate I know that i can provide it using "get position" and so on.

Thanks!

LogLady

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #3 on: October 22, 2014, 07:55:29 AM »
Hi!

I couldn't figure how to send messages to this script (I watched the video about it but the script is different from this) and couldn't create a FSM to simulate this script and track more than 2 different objects. Can somebody help me?

LogLady

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #4 on: November 22, 2014, 05:53:30 AM »
Any news?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #5 on: January 12, 2015, 05:49:00 AM »
Hi,

I concur with Lane. I would use the behavior as is, it involves sufficient math to leave it as is instead of trying to make an fsm and a set of custom for this.

For learning purposes, this behavior is in my opinion already something for advanced users in terms of design.

However, I think it's important to cover also advnaced case, not just "hello worlds". Let me work on a proper set of actions and a sample to go on the ecosystem to do exactly this.

 Bye,

 Jean

LogLady

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #6 on: January 12, 2015, 11:44:08 AM »
Maybe I'm using the script control actions wrongly but I can't figure how to control this script in particular like sending the values and stuff.

Great to hear this Jean! I'm looking forward for this!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12351
  • Official Playmaker Support
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #7 on: February 19, 2015, 02:22:28 AM »
Hi,

 ok, made an action for this.

 you can get it on the Ecosystem or download it below:

 CameraOrthoTrackTargets

LogLady

  • Full Member
  • ***
  • Posts: 125
    • View Profile
Re: Arena camera - How to do this in Playmaker
« Reply #8 on: February 23, 2015, 02:11:37 PM »
Jean, THANK YOU!!!

It is working perfectly and I'm pretty sure that it will be of use to the whole community.

THANK YOU!  :) :) :)