playMaker

Author Topic: Bullet Ricochet  (Read 1501 times)

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Bullet Ricochet
« on: March 23, 2020, 07:20:14 PM »
Hello,

Hope everyone is having a fantastic day so far!

I was hoping someone with experience is bored and could help me convert a script into states of an FSM? Currently the script works fine id just like to learn how to do it using actions. It's a small script and I'll keep trying if no one replies :D and if I ever get it I'll post the answer here as well ;) If someone is kind enough to help it would be greatly appreciated!

Code: [Select]
public class Bullet : MonoBehaviour
{
    public Transform bullet;
    private float speed = 5;
   
    void Update()
    {
        transform.Translate(Vector3.forward * Time.deltaTime * speed);
       
        Ray ray = new Ray(transform.position, transform.forward);
        RaycastHit hit;
       
        if (Physics.Raycast(ray, out hit, Time.deltaTime * speed + .5f))
        {
            Vector3 reflectDir = Vector3.Reflect(ray.direction, hit.normal);
            float rot = 90 - Mathf.Atan2(reflectDir.z, reflectDir.x) * Mathf.Rad2Deg;
            transform.eulerAngles = new Vector3(0, rot, 0);
       
        }
    }
}

Background: There is a sphere in the scene (as my bullet) and a cube (as the wall) if the sphere hits the wall head on it will ricochet straight back. If the sphere is on an angle it would bounce off the wall at an angle which is pretty sweet.
Have I said how much I love playmaker!!! This is one amazing tool

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Bullet Ricochet
« Reply #1 on: March 23, 2020, 07:43:26 PM »
FSM added to the sphere and Actions part1 (Image attached):

Added Translate with a simple speed variable (of 3)

Added Raycast action:
-Game Object: Use owner
-Direction: x0, y0, z1 (needed to work (direction of the raycast)).
-Distance: 1 (how long the ray is).
-Hit Event: hitEvent (storing to be able to change states, when the ray hits)
-Store Hit Normal: hitNormal (needed later)
-Repeat Interval: 2 (set as high as possible as casting rays to often is a performance hit).
-Debug: Checked (so I can see it in the scene).

More to come...
Have I said how much I love playmaker!!! This is one amazing tool

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Bullet Ricochet
« Reply #2 on: March 24, 2020, 07:59:42 AM »
Hi.
I converted the script into an action.
I did not test this.

I used the 'Translate' action as a base to set OnPreprocess etc and changed values to fsmvalues so they work as an action.

Have a look at the script (that way i actually leaned c#, by looking to the action scripts)

let me know if it worked and/or if you need other values exposed to manipulate

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: Bullet Ricochet
« Reply #3 on: March 24, 2020, 06:25:27 PM »
Whoa! Thats awesome! Thank you!
For some reason the update function did not work but that's ok I can use late update. I also added a simple length variable to the raycast so it can be adjusted (please see attached).

I'm short on time today but wanted to at least say thank you (I really do appreciate it), its neat to see how custom actions work, hopefully tomorrow night or this weekend I can play around with it some more.

P.S. glad to see your are eager to learn, as am I  :)
« Last Edit: March 24, 2020, 06:27:01 PM by Kodagames »
Have I said how much I love playmaker!!! This is one amazing tool

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Bullet Ricochet
« Reply #4 on: March 25, 2020, 06:43:07 AM »
Hi.
At the moment i am already quite comfortable with c# but its all thanks to playmaker.

6 years ago there where not that many actions yet, so many times there was just no action to find for certain things.
So i started to look in to the actions and slowly started to understand how c# works.
Before that i had only very little knowledge of programming and hit to many walls and actually quit for a while (until i found Playmaker)