playMaker

Author Topic: [Custom Action] Simple RTS Camera (2D)  (Read 174 times)

MlleBun

  • Playmaker Newbie
  • *
  • Posts: 25
    • View Profile
[Custom Action] Simple RTS Camera (2D)
« on: April 04, 2021, 01:23:54 PM »
Hi,

My first contribution to this forum (without knowing if a custom Action already exists).

This is a very basic (and rapidly scripted) RTS Camera movement in 2D (X and Y axis only for now).

  • I use it on my Main Camera
  • If Panning Limit is checked, you can fill the seperate Left/Right/Up/Down limits


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2021. All rights reserved. 
// Thanks to @Brackeys and @Playmaker

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory("Camera")]
    [Tooltip("RTS Camera with panning limits")]

    /* ------------------------------------------------
     * RTS_Camera V.0
     * ------------------------------------------------
     * [04.04.2021]
     * Added Basic RTS Camera working on 2D XY axis
     *
     *
     * ------------------------------------------------
     * TO DO
     * ------------------------------------------------
     * - Top Down Cam (XZ axis)
     * - Smooth camera movement
     * - Implement Keyboard Inputs
     * ------------------------------------------------
    */

    public class RTS_Camera : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The GameObject to move when camera is out of screenedge limits")]
        public FsmOwnerDefault gameObject;

        [Tooltip("Edge Limits")]
        public FsmFloat screenEdgeSize;
        [Tooltip("Pan speed")]
        public FsmFloat panningSpeed;

        [Tooltip("If PanLimit is true, the panning is clamped to Pan Limit x and y")]
        public bool panningLimit;
        [Tooltip("If PanLimit is true, Left border clipping")]
        public FsmFloat limitLeft;
        [Tooltip("If PanLimit is true, Right border clipping")]
        public FsmFloat limitRight;
        [Tooltip("If PanLimit is true, Up border clipping")]
        public FsmFloat limitUp;
        [Tooltip("If PanLimit is true, Down border clipping")]
        public FsmFloat limitDown;

        // Mouse & Target position
        private float mouseX, mouseY;

        // Screen Aspects
        private float screenWidth, screenHeight;

        public override void Reset()
        {
            gameObject = null;
            panningSpeed = 5f;
            screenEdgeSize = 100f;
            limitLeft = -5f;
            limitRight = 5f;
            limitUp = 5f;
            limitDown = -5f;
        }

        public override void OnEnter()
        {
            GetScreenSize();
        }

        public override void OnUpdate()
        {
            GetMousePosition();
            SetPosition();
        }


        // Get and Store Screen Aspects
        void GetScreenSize()
        {
            screenWidth = Screen.width;
            screenHeight = Screen.height;
        }


        // Set gameObject position
        private void SetPosition()
        {
            // Current gameObject position
            var go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null)
            {
                return;
            }

            Vector3 pos = go.transform.position;


            // Screen X limits
            if (mouseX >= screenWidth - screenEdgeSize.Value)
            {
                pos.x += panningSpeed.Value * Time.deltaTime;
            }

            if (mouseX <= screenEdgeSize.Value)
            {
                pos.x -= panningSpeed.Value * Time.deltaTime;
            }

            // Screen Y limits
            if (mouseY >= screenHeight - screenEdgeSize.Value)
            {
                pos.y += panningSpeed.Value * Time.deltaTime;
            }

            if (mouseY <= screenEdgeSize.Value)
            {
                pos.y -= panningSpeed.Value * Time.deltaTime;
            }


            // Pan Limits
            if (panningLimit)
            {
                pos.x = Mathf.Clamp(pos.x, limitLeft.Value, limitRight.Value);
                pos.y = Mathf.Clamp(pos.y, limitDown.Value, limitUp.Value);
            }

            go.transform.position = pos;

        }


        // Get Mouse Position
        private void GetMousePosition()
        {
            float xpos = Input.mousePosition.x;
            float ypos = Input.mousePosition.y;

            mouseX = xpos;
            mouseY = ypos;
        }
    }
}

Feel free to use/repost bugs/improve it and generously post your contribution ;)


« Last Edit: April 04, 2021, 01:25:49 PM by MlleBun »