How to cast to the location of a separate Actor on an input event?

Hey all,

I’m a very, very novice programmer, who just wrapped up a Udemy course on C++ with Unreal. Despite that, I’m finding the Blueprints more and more easy to use, so I guess that’s that.

Anyway, I decided to go without too much direct instruction for a while and try my hand at implementing something of my own. For my first effort I settled on making a very, very rough mechanic of a movement system from this book series, Mistborn. Basically, my goals are:

  1. Make it so the character can throw a coin in whatever direction they’re looking (done!)
  2. Make it so the character can press a separate key and be flung in the opposite direction of the last coin they threw.

I found out that there’s a Launch Character function, which is superb, but getting the launch velocity for it is proving tricky. In my mind, the best way to get this would be to get the Vector from the most recently thrown coin to the ThirdPersonController, then use that to get a Velocity and then fling the player. But I just can’t seem to find a good way to get the location of the last coin thrown. I’d use a cast and then GetActorLocation, but I’m struggling to find a way to plug an “Other” object into the cast. The InputAction event doesn’t have that object node to plug into a cast, I don’t fully understand why that is, and I’m a bit stuck on how to progress.

So, TL;DR: how can I get the location of an object in the world on an input event to get a Vector to launch the player?

(bonus question, I’m not sure how I would specify it to only grab the location of the most recently spawned coin, so if anyone has any advice in that department please let me know thanks)

To get the direction from 2 actors Actor.Loc - actor2.Loc then multiply then convert into unit vector then multiply it, then enjoy the launch function.

You mean do this in C++? I can just do this .Loc thing, subtract them, and get a direction, then multiply it and convert to vector? Is there a way to do it through blueprint?

I told you a concept, now try to apply it on blueprints.

Actor.GetAcforLocation - Actor2.GetActorLocation then multiply it.

Top bit is the coin toss, bottom is the launch:

Script to copy:


how can I get the location of an object in the world on an input event to get a Vector to launch the player

You could store the location only as a vector but then you’d need to wait for the coin to stop moving. So we store a reference to the spawned actor instead. One thing that’d need adding here before jumping away is to check whether last coin is valid - perhaps something scooped it up! Better safe than sorry (and coinless).

1 Like

there is multiple ways to do it, though I’ve learn’t a new one it seems that RotateVectorAroundAxis… is kind off new lol.
But I stick to the tradiational way vector - vector …

That’d give you another vector only. But you may not be happy with said direction (after normalising) and want to modify it a bit.

1 Like

No that does give you the direction… lemme lurk my code, sorry if I did say something wrong : O


This works for me. Kicking launching a player.

Right, I get how this works, but my question was how to even get the location of the second actor. I know how to get the vector once I have the locations, I was just confused on how to get the locations in the first place.

Thanks so much for the help!

1 Like

Whenever something is spawned, we store a reference - it stores only the last spawned coin. There is an explanation above, too.

1 Like

Or with getAllactors you get that specific actor! L : all roads go to rome, but there is many not only 1 : P

You cannot. It does not guarantee order.

1 Like

Yes you can, just declare a variable on it with a specific value that is what is a variable on it’s core, But hey do not use my way as it is expensive and stupid… LoL

Agree, one can but absolutely should not rely on GetAll to get a specific element out of an array. Please avoid - every 10th run or so you’ll get elements in a different order and open the gate to a little debugging hell. Reference stuff.

Tis all, my 2 ¢oins ;p

1 Like

So I gave this a shot and I’m still having an issue now, I can set the Last Thrown Coin but then I can’t seem to do the Get Static Mesh off of it (it just won’t show up?) previously I was getting the capsule component but that isn’t showing up either. Any idea why that might be?

EDIT: It saying “Object Reference is not compatible with Object Reference” is really throwing me for a loop

Seems like the issue is that my BP_Coin isn’t compatible. Still a bit confused but on the right track I think

Right click the blue pin:

image

And promote to variable. This will create a reference variable for the correct type.

1 Like

It worked! One final question, I think. Got a bit of a bug and I’m hella confused

I noticed that as I was pressing the key to launch, sometimes I was being sent in a direction completely away from where the coin was. When I pressed F8 to check it out and selected the coin, I found that the static mesh of the coin is way over there while the transform (is that the root?) is in a completely different place. I would think that because I childed the static mesh to the root component, that they would stay together, but that doesn’t seem to be the case. What’s also weird is that it seems like, despite using the static mesh in the launch, that when I press the key I’m pushing away from the root component and not the static mesh. Any idea why this could be happening?

EDIT: Figured it out! Just replaced the root component with the collision. Weird