playMaker

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

serenefox

  • Full Member
  • ***
  • Posts: 125
    • View Profile
    • Serenefox Games
[Solved] Projectiles are deflecting off of targets
« on: January 22, 2019, 11:19:55 PM »
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, 07:48:17 PM by serenefox »
Most Recent Published Games:
Don't Pop! Dungeon - iOS/Android

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14564
  • Official Playmaker Support
    • View Profile
Re: Projectiles are deflecting off of targets
« Reply #1 on: January 23, 2019, 12: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: 125
    • View Profile
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #2 on: January 23, 2019, 10:09:03 AM »
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:
Don't Pop! Dungeon - iOS/Android

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: Projectiles are deflecting off of targets
« Reply #3 on: January 23, 2019, 09: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: 125
    • View Profile
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #4 on: January 23, 2019, 10:55:10 PM »
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:
Don't Pop! Dungeon - iOS/Android

GonerGames

  • Junior Playmaker
  • **
  • Posts: 51
    • View Profile
Re: Projectiles are deflecting off of targets
« Reply #5 on: January 24, 2019, 09: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: 125
    • View Profile
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #6 on: January 24, 2019, 02: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:
Don't Pop! Dungeon - iOS/Android

GonerGames

  • Junior Playmaker
  • **
  • Posts: 51
    • View Profile
Re: Projectiles are deflecting off of targets
« Reply #7 on: January 25, 2019, 01: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: 125
    • View Profile
    • Serenefox Games
Re: Projectiles are deflecting off of targets
« Reply #8 on: January 25, 2019, 07: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:
Don't Pop! Dungeon - iOS/Android

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 4582
    • View Profile
    • jinxtergames
Re: [Solved] Projectiles are deflecting off of targets
« Reply #9 on: January 26, 2019, 03: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: 125
    • View Profile
    • Serenefox Games
Re: [Solved] Projectiles are deflecting off of targets
« Reply #10 on: January 26, 2019, 09:51:14 AM »
Hi djaydino,

Yeah it happened in builds too.
Most Recent Published Games:
Don't Pop! Dungeon - iOS/Android