playMaker

Author Topic: Scripting // strange Velocity behaviour  (Read 1257 times)

Thore

  • Sr. Member
  • ****
  • Posts: 480
Scripting // strange Velocity behaviour
« on: September 27, 2018, 09:17:01 AM »
Hello,

I am attempting an AddExplosionForce2D, but it shows some strange behaviour. Can somebody with scripting knowledge give me an idea what might be the issue?

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2018. All rights reserved.
// Made by Thore: change anything you like, but if you do, you must stroll in a nearby park.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Physics2D)]
    [Tooltip("Adds velocity to targets based on direction to explosive. Useful to create a blast effect.")]
    public class AddExplosionForce2D : ComponentAction<Rigidbody2D>
    {

        // BASE VARIABLES
        [RequiredField]
        [Tooltip("GameObject from where the force is coming from.")]
        public FsmOwnerDefault explosive;

        [RequiredField]
        [CheckForComponent(typeof(Rigidbody2D))]
        [Tooltip("To blow up, must have a Rigidbody2D attached.")]
        public FsmOwnerDefault target;

        [RequiredField]
        [Tooltip("The power of the sideways blast (uses velocity).")]
        public FsmFloat blast;
       
        [RequiredField]
        [Tooltip("Adds additional velocity.y force up.")]
        public FsmFloat blowup;

        private float direction;
         private Vector2 velocity;

        [Tooltip("Repeat every frame. Are you sure?")]
        public bool everyFrame;

        private Rigidbody2D rb;

        // STANDARD METHODS

        public override void Reset()
        {
            explosive = null;
            target = null;
            everyFrame = false;
            blast = null;
            blowup = null;
            direction = 0f;
            velocity = Vector3.zero;


        }

        public override void OnEnter()
        {
            BlastAway();

            if (!everyFrame)
            {
                Finish();
            }
        }

        public override void Awake()
        {
            Fsm.HandleFixedUpdate = true;
        }

 
        public override void OnUpdate()
        {
            BlastAway();
        }


        ///  BOOM! ///////////////////////////////////////////

        void BlastAway()
        {

           



            // Get Direction
            var posExplosive = Fsm.GetOwnerDefaultTarget(explosive).transform.position;
            var posTarget = Fsm.GetOwnerDefaultTarget(target).transform.position;

            direction = Mathf.Abs(posTarget.x) - Mathf.Abs(posExplosive.x);

            var isTargetRight = direction < 0;

            Debug.Log("Target is Right? " + isTargetRight);

            // Get Rigidbody2D
 

            var to = Fsm.GetOwnerDefaultTarget(target);
            rb = to.GetComponent<Rigidbody2D>();

            if (!UpdateCache(to)) // no idea what this does, but found it in official action.
            {
                return;
            }


            // Blast Away


            if (isTargetRight)
            {
                velocity.y = blast.Value + blowup.Value;
                velocity.x = 5;
                rb.velocity = velocity;

            }

           else
            {
                velocity.y = blast.Value + blowup.Value;
                velocity.x = -5; // Bug is here.  Once this is active, the object's vector seems to flip, and keeps orientation, resulting in hopping back and forth.
                rb.velocity = velocity;
               
            }



        } // END BlastAway



    } // END Class



} // END Namespace

I now hardcoded the 5/-5 values in there, to be clear what is going on (that's later a variable). The bug is likely in 121.

What it's supposed to do
When the target is left of explosive, blast it to the left, and when the target is right, blast it to the right direction. The direction itself isn't the issue (I made a custom action which works).

What it does wrongly
As long as I comment out the blast part, the direction is shown correctly (in log). It should therefore plug in -5 when the target is one direction, and +5 when it is in the other direction. However, when the target is to the right side, it strangely alternates directions with each blast. I can only assume that the velocity vector is somehow flipped and this orientation is not reset, but I am clutching straws.

Test Scene
I added a test scene with the action attached below.