playMaker

Author Topic: playmaker custom action - chatGPT can't solve this for me  (Read 498 times)

sturgical

  • Playmaker Newbie
  • *
  • Posts: 5
playmaker custom action - chatGPT can't solve this for me
« on: July 13, 2023, 02:31:46 AM »
Hey folks,

I'm not a programmer (which is why I love playmaker).  I'm sure I'm not alone in that I've started asking chatGPT to help me solve playmaker problems.  It's been great, but it can sometimes get me out over my skies, so to speak.

I'm trying to make an interactive crank that can be spun by mouse movement.  I've managed, leaning very much on the assistance of chatGPT, to create a custom playmaker action that rotates the crank.  It uses mouse movement to rotate the crank object in the z axis in the FSMs 'Drag' state.  When I let go it returns to the 'Idle' state. 

This is exactly what I want except for one thing.  Every time I grab the handle the crank jumps ahead in it's rotation by 90 degrees or so.  I really just want the crank to turn smoothly from the start, and it feels like I'm so close.

I realize this is more of a code question and less a playmaker one, but I was hoping someone could find our (good ol' GPT and my) error here in the code.

Thanks in advance for any help! 

Here's the flawed code:

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

namespace MidlifeCrisis
{
    [HutongGames.PlayMaker.Tooltip("Rotate the crank handle based on mouse movement.")]
    public class RotateCrankHandleAction : FsmStateAction
    {
        // Sensitivity to control the rotation speed
        public float rotationSensitivity = 1.0f;

        // Store the initial mouse position
        private Vector3 initialMousePosition;

        private bool isDragging = false;
        private bool hasMoved = false;

        // Reference to the Playmaker FSM float variable
        private FsmFloat crankCurrentRotation;

        private float initialRotationAngle = 0f;
        private float rotationOffset = 0f;

        public override void OnEnter()
        {
            base.OnEnter();

            // Retrieve the Playmaker float variable reference
            crankCurrentRotation = Fsm.Variables.GetFsmFloat("crankCurrentRotation");

            // Store the initial rotation angle if it's not already set
            initialRotationAngle = crankCurrentRotation.Value;

            // Get the initial mouse position in screen space
            initialMousePosition = Input.mousePosition;

            // Set the dragging flag to true
            isDragging = true;
            hasMoved = false;
        }

        public override void OnUpdate()
        {
            base.OnUpdate();

            if (isDragging)
            {
                // Get the current mouse position in screen space
                Vector3 currentMousePosition = Input.mousePosition;

                // Calculate the delta mouse position
                Vector3 deltaMousePosition = currentMousePosition - initialMousePosition;

                // Normalize the delta mouse position vector
                deltaMousePosition.Normalize();

                // Check if the mouse has moved
                if (deltaMousePosition.magnitude > 0f)
                {
                    hasMoved = true;

                    // Calculate the rotation angle based on the delta mouse position
                    float rotationAngle = Mathf.Atan2(deltaMousePosition.y, deltaMousePosition.x) * Mathf.Rad2Deg;

                    // Apply the rotation sensitivity to the rotation angle
                    rotationAngle *= rotationSensitivity;

                    // Apply the rotation offset by subtracting the initial rotation angle
                    rotationOffset = rotationAngle - initialRotationAngle;

                    // Apply the rotation to the crank handle's Z-axis rotation
                    // Assuming you have a reference to the crank handle object
                    GameObject crankHandleObject = Owner.gameObject;
                    crankHandleObject.transform.rotation = Quaternion.Euler(0f, 0f, initialRotationAngle + rotationOffset);
                }
            }

            // Check for mouse button release
            if (!Input.GetMouseButton(0) && isDragging)
            {
                // Set the dragging flag to false
                isDragging = false;

                // Trigger the transition to the Idle state only if the mouse has moved
                if (hasMoved || Input.mousePosition != initialMousePosition)
                {
                    // Update the crankCurrentRotation variable in Playmaker
                    crankCurrentRotation.Value = initialRotationAngle + rotationOffset;

                    Fsm.Event("HandleReleased");
                }
            }
        }
    }
}



Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: playmaker custom action - chatGPT can't solve this for me
« Reply #1 on: July 13, 2023, 12:37:09 PM »
Try this. Also done with chatGPT:

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Transform)]
    [Tooltip("Rotates an object based on mouse movement.")]
    public class RotateObjectWithMouse : FsmStateAction
    {
        [RequiredField]
        [Tooltip("The object to rotate.")]
        public FsmGameObject objectToRotate;

        [Tooltip("The rotation speed.")]
        public FsmFloat sensitivity;

        [Tooltip("The rotation axis.")]
        public AxisEnum rotationAxis;

        [Tooltip("Event to send when the mouse click ends.")]
        public FsmEvent clickEndEvent;

        private bool isRotating;

        public enum AxisEnum
        {
            XAxis,
            YAxis,
            ZAxis
        }

        public override void Reset()
        {
            objectToRotate = null;
            sensitivity = 1f;
            rotationAxis = AxisEnum.YAxis;
            clickEndEvent = null;
        }

        public override void OnEnter()
        {
            isRotating = false;
        }

        public override void OnUpdate()
        {
            if (Input.GetMouseButtonDown(0))
            {
                isRotating = true;
            }
            else if (Input.GetMouseButtonUp(0))
            {
                isRotating = false;
                Fsm.Event(clickEndEvent);
            }

            if (isRotating)
            {
                float rotationAmount = Input.GetAxis("Mouse X") * sensitivity.Value * 100f;

                Quaternion rotation = Quaternion.identity;

                switch (rotationAxis)
                {
                    case AxisEnum.XAxis:
                        rotation = Quaternion.Euler(rotationAmount, 0, 0);
                        break;
                    case AxisEnum.YAxis:
                        rotation = Quaternion.Euler(0, rotationAmount, 0);
                        break;
                    case AxisEnum.ZAxis:
                        rotation = Quaternion.Euler(0, 0, rotationAmount);
                        break;
                }

                objectToRotate.Value.transform.rotation *= rotation;
            }
        }
    }
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: playmaker custom action - chatGPT can't solve this for me
« Reply #2 on: July 13, 2023, 02:44:25 PM »
Hi.
its all about asking the right questions and give correct details.

eotiti

  • Junior Playmaker
  • **
  • Posts: 65
Re: playmaker custom action - chatGPT can't solve this for me
« Reply #3 on: July 14, 2023, 04:40:18 AM »
Wow .... Chat GPT can make the action playmaker ?

sturgical

  • Playmaker Newbie
  • *
  • Posts: 5
Re: playmaker custom action - chatGPT can't solve this for me
« Reply #4 on: July 14, 2023, 11:57:23 AM »
Hey Christoph,

Thanks for the code!  It isn't working for me though.  My object stubbornly refuses to rotate.  Am I using it correctly?
I have a state in which the crank is not interactive....you 'connect' to it and then it alternates between idle and drag states.  I added your action to the drag state, but I'm not seeing any movement at all.  I tried changing the sensitivity and axis values, but no luck.


Attaching a screenshot of my FSM setup.  Would really appreciate any advice.

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: playmaker custom action - chatGPT can't solve this for me
« Reply #5 on: July 14, 2023, 03:34:25 PM »
For the action like it is now it needs to be in that state when you start to click/dragging.

How do you currently 'connect' to the crank?

Try to go backwards. Just add the action to the start state and enter playmode. Then you'll understand how it works now and if you need to make adjustments or not.

sturgical

  • Playmaker Newbie
  • *
  • Posts: 5
Re: playmaker custom action - chatGPT can't solve this for me
« Reply #6 on: July 17, 2023, 11:54:24 AM »
Oh got it.  It works!  Thanks, Christoph!

However this only takes into account x movement of the mouse.  Great for spinning a character around to admire. 

I'm trying to create a crank with a handle you can drag around in clockwise fashion...so I need to take into account mouse x and y movement.  And I need to be able to drag the handle in a complete circle, so x movement/input can't just be positive or negative. 

Anyone have advice for this sort of script?  Or fixes for my pasted script above?

Thanks!