playMaker

Author Topic: [Solved] Projectiles are deflecting off of targets  (Read 3135 times)

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
[Solved] Projectiles are deflecting off of targets
« on: January 23, 2019, 01:19:55 AM »
Hi,

I think this is more of a unity question but I figured I would ask here since I am using Playmaker. I have an FSM on my projectile that listens for the collisions. Most of the time this works fine. Although if the projectile hits a target at the right angle it will deflect and just either float there or hit something else and despawn. I have all my tags and layers set up correctly but sometimes a projectile will deflect and most times it works. My game is a 2d twin stick shooter.

Note* it is rare when playing that this happens but I can reproduce it by position my player in just the right spot. I have attached and image of the problem. In the image this time one of the enemies fired a projectile and it deflected off of my player.

I shoot a lot of projectiles in my game at one time as well so I'm not sure raycasting is the way to go. I also can't use triggers (i dont think) because I need to use "get collision2d info" to spawn particles and use it for knockback calculations.

Thanks for any advice.
« Last Edit: January 25, 2019, 09:48:17 PM by serenefox »
Most Recent Published Games:
Juicy Theater - Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Projectiles are deflecting off of targets
« Reply #1 on: January 23, 2019, 02:23:59 AM »
Hi,

can you disable the collision listener in your fsm as soon as you have hit something? this is likely the source of the trouble.

 whe you find such faulty projectile, pause immediatly, and step back in that fsm to see what went wrong, you will likely see that you got two collision callbacks ( checck the fsm log, it will show that too)

Bye,

 Jean

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #2 on: January 23, 2019, 12:09:03 PM »
Hi Jean,

I checked the log before and after the deflection and it is empty. Also my collider event2d never got called on the state it was on. I think it has something to do with minimum penetration of the collision. I tried changing the "default contact offset" in the project settings on the 2d physics but it still happens a bit.

If there isn't a fix for this, is there a better way to do these projectiles and still get the collision information?
Most Recent Published Games:
Juicy Theater - Android

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: Projectiles are deflecting off of targets
« Reply #3 on: January 23, 2019, 11:49:45 PM »
Hi.
I recently answered some question with a weapon that had issues also.

as with triggers you can't get the collission. But if i understand right, when you set "Is Kinemantic" you can pass thru an object, but still get collision data.

So maybe that can be a solution :)

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #4 on: January 24, 2019, 12:55:10 AM »
Hi djaydino,

I use the physics system to move my projectiles with add force2d so unfortunately that's not an option.
Most Recent Published Games:
Juicy Theater - Android

GonerGames

  • Junior Playmaker
  • **
  • Posts: 53
Re: Projectiles are deflecting off of targets
« Reply #5 on: January 24, 2019, 11:01:13 AM »
This is more likely a Unity issue as your log isn't even registering the event.
Unity's default physics calculation happens at 50 fps. If your game is running higher than 50fps, then these issues can happen. Especially with multiple events per frame.

In Edit -> Project Settings -> Time you can adjust the Fixed Timestep value. The calculation is 1/fps so for 60 fps you would use 1/60 = 0.0166666
Note that the lower the number the higher the CPU overhead will be.

Mobile:
If this is for mobile then 0.01666 (60fps) is probably the lowest you want to set this.
Also for the Maximum Allowed you'll want this between 8-10 fps (0.125 - 0.1)

Hope this helps



serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #6 on: January 24, 2019, 04:19:22 PM »
Hi GonerGames,

I tried the suggested values but the projectile still deflected in the same manner. I had thought about the Timestep but hadn't tried it yet, it was a good idea though.
Most Recent Published Games:
Juicy Theater - Android

GonerGames

  • Junior Playmaker
  • **
  • Posts: 53
Re: Projectiles are deflecting off of targets
« Reply #7 on: January 25, 2019, 03:18:06 PM »
Sorry that didn't work for you.

We recently came across this problem in an in house project we are working on as well. It seemed that the Playmaker collision event detection didn't quite keep up or wasn't being notified. Unity would make the collision happen, but Playmaker wouldn't fire the event. Timestamp worked at first to solve, but then at faster speeds the issue came back. To solve in the meantime we have the below in a c# script attached our object to detect collisions. It was more reliable than the Playmaker detection. I am not a Playmaker custom action expert so I can't explain why this was more efficient in our case.

To setup:
1. put script on bullet
2. Drag the bullet object onto the "go" reference in the script.
3. create a bool variable in your fsm called "hit"
4. Just have a bool test state where your collision detection would go and have the bool switch to true from the script. Remember to turn it back to false in the next state :)

Code: [Select]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HutongGames.PlayMaker;



    public class Tester : MonoBehaviour {

    public PlayMakerFSM go;

    //check for 3D collision

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "[Tag here]") //put tag name here alt - collision.gameObject.name == "[name here]" can also be used
        {
            //get our variable from the FSM
            FsmBool hit = go.FsmVariables.GetFsmBool("hit");

            //turn value to true
            hit.Value = true;

            //Log event to be sure
            Debug.Log("Bullet hit");
        }
    }


    //check for 2D collision
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "[Tag here]") //put tag name here alt - collision.gameObject.name == "[name here]" can also be used
            {

            //get our variable from the FSM
            FsmBool hit = go.FsmVariables.GetFsmBool("hit");

            //turn value to true
            hit.Value = true;

            //Log event to be sure
            Debug.Log("Bullet hit");
        }
    }

}

I still think this is more of a Unity issue than Playmaker.

Hope this helps  ;D

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #8 on: January 25, 2019, 09:47:36 PM »
Hi GonerGames,

I tried your test script and modified it to fix my game. I can’t believe it worked to be honest. I will have to look at the playmaker action for collision enter 2d and see what the differences are. That must mean there is some conflict between playmaker and unity of some type or the information isn’t being passed to playmaker like you said. But thanks for the help!
Most Recent Published Games:
Juicy Theater - Android

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7615
    • jinxtergames
Re: [Solved] Projectiles are deflecting off of targets
« Reply #9 on: January 26, 2019, 05:25:49 AM »
Hi.
Does it happen also in a build? or only in editor?

there can be bad behavior, especially when the playmaker editor window is open and/or fsms in the inspector.

serenefox

  • Full Member
  • ***
  • Posts: 135
    • Serenefox Games
Re: [Solved] Projectiles are deflecting off of targets
« Reply #10 on: January 26, 2019, 11:51:14 AM »
Hi djaydino,

Yeah it happened in builds too.
Most Recent Published Games:
Juicy Theater - Android