playMaker

Author Topic: [SOLVED]Create referenced Instantiate from Raycast?  (Read 1728 times)

curb47

  • Sr. Member
  • ****
  • Posts: 256
[SOLVED]Create referenced Instantiate from Raycast?
« on: July 13, 2020, 10:38:13 AM »
Hi there,
This is a tricky one... huge kudos to anyone who can solve it...

I'd like to shoot lasers from my spaceship. At the moment I have an Empty Object at the tip of the gun barrel, which serves as the origin for the spawn point, and I have a Draw Line (Line Render) there too.

The Empty Object is a child of a spaceship, so it moves around a lot.

When the Fire button is pressed, the Raycast returns a Store Hit Point variable.

I would like to somehow create an Instantiated object, at the Store Hit Point vertex, and also Instantiate the Empty Object.

I would then draw a line between these two points, that are now fixed in World space, and are not linked to the spaceship. Make sense?

Basically, I was the ship to have rapid fire lasers, but the lasers are not a child of the ship, so the lasers don't move as the ship whizzes around.

I've gotten somewhere with the Create Object action, but I can't seem to get Draw Line to reference the instantiated objects.

I've searched high and low for a solution to this, and I'm hitting dead ends everywhere.

Any help is greatly appreciated.
« Last Edit: July 17, 2020, 08:34:35 AM by djaydino »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Create referenced Instantiate from Raycast?
« Reply #1 on: July 13, 2020, 01:21:40 PM »
Hi.
You can make a prefab with 2 child objects in it (point A, Point B)
On the parent of this prefab have a fsm to handle positioning and Draw Line

Best is to use a global transition (call it 'Set Laser' for example) to do the set positions and draw line.
The Start state should be empty

When you Create the Prefab, store it in a variable.
Then use 2x 'Set Fsm Vector 3' to send the tip position and Store Hit Point from the raycast (point A, Point B) to the created object.
Then use a 'Send Event By Name' and also target the Prefab and send the 'Set Laser'

Make sure all the Get and Set Positions are in world space.

As you probably gonna be shooting many laser you might want to look into pooling.
Pooling is way more efficient that Create/Destroy

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #2 on: July 14, 2020, 12:54:13 AM »
Hi dino,

Thank you for that detailed explanation! I hadn't thought of creating a prefab for the laser, that makes sense.

I'll give it a go and no doubt get back to you with another problem!

Thanks again.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #3 on: July 14, 2020, 01:16:05 AM »
Holy moly,

okay, I'm tackling this, but I think I need my hand held and walked through this a bit more... sorry.

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #4 on: July 14, 2020, 01:34:04 AM »
I found this tutorial which is pretty helpful... but still... my brain is slowly melting... again...

https://www.youtube.com/watch?time_continue=2&v=0D815G0Uwkw&feature=emb_logo

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Create referenced Instantiate from Raycast?
« Reply #5 on: July 14, 2020, 02:57:49 AM »
Hi.
For this case you do not need a 'Game Manager' to connect to.

On the video, the prefab 3 type is the style you should use for this.

Before sending the event to 'Activate' you 1st need to send the position data by using Set Fsm Vector 3 (for point A and B)
After that you can send the 'Activate' event.

and on the prefab, on 'Activate' do set position to Point A and B
Then do Line Cast.


This video explains about transitions :


curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #6 on: July 16, 2020, 10:58:14 AM »
Hi Dino,

Hope you're well. Thanks for points me in the right direction with the lasers, I now have red lasers zapping out of my spaceship at the correct worldspace, and they look exactly how I wanted (well, nearly).

However, I have reached a result whilst missing out (due to confusion) one or two steps from your instructions, which niggles me because I'm sure you have best-practice coding in mind, so I'd like to really nail what you instructed and refine what I've done.

The areas that confused me are:

Best is to use a global transition (call it 'Set Laser' for example) to do the set positions and draw line.
The Start state should be empty
- I have set a global transition, but not sure how to implement it.

When you Create the Prefab, store it in a variable. - I have done this, but not sure what to do with it.

Then use a 'Send Event By Name' and also target the Prefab and send the 'Set Laser' - Just general confusion.

At the moment, my set up is this:

I have a spaceship, with an empty GameObject positioned at the tip of the laser cannon.  This GameObject is called Laser1.

On Laser1 I have an FSM:

> Start

> State 1 - Get Key Down - M. When M is pressed go to state 2.

> State 2 - (screen grabs below).





I have a simple third state for the Hit Event in the RayCast, but it's empty and not important at this stage.


Okay. Next I have the Prefab.

As you suggested, I made one GameObject as the Parent, and 2 Children inside: startpoint and endpoint.

On the Parent I have one FSM, called FIRE:



This Parent object was then dragged down into my Prefabs folder, to create the Prefab. This Prefab is renamed to Laser2.

I deleted the original in the hierarchy window, then dragged the Prefab up to take it's place.

On the whole, this entire set-up has moved me in the direction I desire, with loads of lasers shooting out, all in the correct place.

I have yet to work out the Pooling technique which will be essential I guess, as the Hierarchy window fills up very quickly with clones prefabs.

Anyway... so back to your instructions... Like I said, I kinda have it working, but feel like I'm going to get a slap round the face at one point if I don't get your technique correct. Please could you help me some more?

Thank you SO much for the assistance you've given, I'm really happy with what you've helped me achieve.











 
« Last Edit: July 16, 2020, 11:05:45 AM by curb47 »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Create referenced Instantiate from Raycast?
« Reply #7 on: July 16, 2020, 11:20:37 AM »
Hi.

You Should use the Stored object in the Set Fsm actions and the send event by name.



Also you should turn on Sequence (right click on a empty space in the action window to select, you will see a v shape in the line between the actions now)
or place create in a separate state as well as the send event.

if not in sequence a state will start from the top, but will not wait until the actions is finished before starting the next state.

Sequence enabled will wait to start the next action until the current is finished.

In most cases you do not need sequence, but create action can be 'slow'

the send event by name should send a event (for example 'Start Beam') that you placed on the 'Laser2'

On the Laser 2 your fsm should look something like this :


curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #8 on: July 16, 2020, 12:45:14 PM »
Dude! You're a legend!

Thank you!

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #9 on: July 16, 2020, 01:39:31 PM »
Hello again dino,

Okay, I'm getting somewhere now. This is great.

I've hit a weird behavior when I turned on Action Sequence as you suggested:




The lasers didn't play at all. I investigated and found the Act Sequence had not moved past Raycast. (It actually didn't move past Get Key Up but I had to turn that off so I could loop the state to take a screen grab).

This is what I saw:



When I turned off Action Sequence, it played okay and lasers shot out.

However...

Out of curiosity, as the game was playing I investigated the Global Transition on the Laser2 Prefab and saw that the event was never firing (flashing green). Even though the lasers were shooting out on the Game window, the Prefab event wasn't firing. That struck me as bad logic, as I expected to the the startBeam event flashing.

No matter how fast I pressed/held down the M key, it just stayed like this:



Something that I'm sure I've done wrong, is the configuration of the Send Event By Name action. Maybe it's the Every Frame bool?



Here's my current overall set up on the Laser Gun Tip:



And here are the object variables, one for the Laser2 prefab, and one that's created by the Create Object action. That all seems correct.





I just can't see what I'm missing... I'm so close but, so far.

Thanks again for your help, I really appreciate it.


« Last Edit: July 16, 2020, 01:52:44 PM by curb47 »

curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #10 on: July 16, 2020, 02:49:55 PM »
For the record, I've got the Pool thing going, using the Pooler package from the ecosystem.

It really makes sense! Another great idea!

Again, thank you.


curb47

  • Sr. Member
  • ****
  • Posts: 256
Re: Create referenced Instantiate from Raycast?
« Reply #11 on: July 17, 2020, 06:42:47 AM »
All good. I think I've got everything working properly now. Looks awesome!