Jump to content
ZhakyDK

Throw a ball - how to?

Recommended Posts

How do I throw an entity when the player holding it presses the left mouse button (LMB)?

 

Okay, so I'm trying to make a smalle ball game, and therefore it is essential that the players can pick up a ball and toss it around. So far I've got the pick-up mechanic working, but from there I need to allow the player to throw the ball. I've chosen a cannonball and pimped it a bit, made it unique so I can add a script to it.

Currently I'm trying to get the script to do the following:

  • When the cannonball is in the players hotbar AND is selected AND the player presses the LMB ->
  • Throw/shoot cannonball in the direction the player currently aims

I can't find a way to run a script when the player has the cannonball selected and presses the LMB, so how do I do that?

Share this post


Link to post
Share on other sites

Unfortunately I cannot figure out myself how to "aim" and instruct the ball where to go.

But at least I think I can get you started.

First to handle the LMB click, you need to create a Custom Control. You can find that under EDITOR -> Game Settings -> Custom Controls

On the right side you see the "?" in a box, kinda looks like a key on your keyboard, you can define inputs as triggers. Since you want to use LMB, that is already setup, so you wanna go to the finger touching the circle one. That is actions.


Create a new action, set Active to off. Name it whatever you want. Try to keep the ID something easy you can remember like "throw_cannonball". Set trigger type to tap.

By default it is set to RELOAD, click the "SET" button and select PRIMARY ACTION (LMB). If you don't see the list, click on default top right. Show hint and hint just decides what shows up on the bottom right side of the player's hud, I guess this is preferred to be setup.

Event Listener for activating the Custom Command we just set to only work with cannonball active in hotbar.

meh_1.png.2679b36ed4dc9086016892840f37642f.png

Script for this event listener

meh_2.png.379da75d2d69e6628e54e0dea859a4e2.png

 

We need another event listener to handle the new Custom Command

meh_3.png.4491e140a16535190c9b4677f581fe86.png

Script for this event listener

meh_4.thumb.png.ca74d366c59accd705638e4bb76d796f.png

It seems like throw likes to take a Entity template only, so I just threw one down and a cannon ball next to it. Linked them with the little dropper.

I added +2 to the Y of the Origin so the cannon ball doesn't just spawn right in the player and "hits" the player which is spawning it.

Direction is what I cannot figure out myself. Hopefully this helps you out a bit.

  • Like 1

Share this post


Link to post
Share on other sites

There is currently no "get aim" logic in the game. I've already asked the devs to implement it however...

  • Haha 1
  • Upvote 1

Share this post


Link to post
Share on other sites

GOOD NEWS!....kinda

I figured out how to set direction for aim. Just not up/down but it will throw it in the direction your facing.

meh1.png.94709ab16d231fcd5b262f6f75bd5424.png

I found this out from here https://math.stackexchange.com/questions/180874/convert-angle-radians-to-a-heading-vector
and thanks to SouLSeuPai from the P1 discord for helping me learn google is still a thing.

 

Basically GET ROTATION entity(Player) returns a Vector (X,Y,Z) with Y being our heading. 0-360 in relation to the world.

Direction of the THROW tile wants a vector as well. It's kinda weird to explain but maybe this image can help.

1200px-2D_Direction_Vectors_svg.thumb.png.c395af0de430a78802ac7ea76f8fb04d.png

Think of it as when your facing forward from spawn, your heading is 0.

So if we get the COS of 0, we get 1

SIN of 0 is 0

So if we think of these as plotting them on the graph above, let's image our Y axis is Cos and our X axis is Sin. Facing forward with an angle heading of 0, we get (0,1)

Now if we turn 90 degrees to the right, our heading is 90.
Cos of 90 = 0

Sin of 90 = 1

So Y axis, go 0 points | X axis, go 1 point. If we plot that on the graph, it points us to the right.

 

 

So to translate this into Ylands, we need to give THROW a Direction vector:
X is gonna be our SIN result
Y is a bit weird(I'm still learning this). Think of 0 being straight forward, anything closer to positive 1 is gonna be upwards with 1 being straight up into the sky. Any value approaching negative 1 would be going down with -1 being straight down.
Z is gonna be our COS result

So we get the Y vector of our GET ROTATION of player. Apply SIN or COS and throw that into the vector values for direction.

Lastly, Speed. I don't fully understand this either but the higher the number, the farther it throws it and the faster it throws it.

EDIT, I forgot to mention. I changed the +2 to a +1 for Origin so it throws kinda from the hands instead of above the head.

meh2.png.dfa05adf2e753b00e8055ef162362b97.png

Edited by Malaka135
  • Like 2

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×