Hi, is there a method to see if a Player is looking at an actor I’ve created? I’m making a race game, and I wanna see if the Player is on the wrong way or not.
Do you use checkpoints? (even if they are hidden and used for laptimes?) If so, just constantly check if the car is approaching the correct checkpoint or not. If they last passed checkpoint 2, and their distance to 2 is increasing while distance to 3 is decreasing, they’re going the right way, if the opposite, they are going the wrong way. Combined with some logic to tell them if they’ve left the track (even if it’s just rumbling the car or whatever) will both try to encourage them to stay on the track and point the right way. However if you set it up this way you won’t have weird things (based on direction) like if the person is wrecking and spins, or is in a very tight turn, it won’t purely be based on what they’re facing but in what direction they’re moving. You could add a further condition to this not to trigger when off the track, or not to trigger when the car is in reverse, etc.
This is a good idea, but how can I trace the distance from the Pawn to the next checkpoint?
This will depend a lot on how your track/checkpoints are implemented, but tracing is covered similarly here https://forums.unrealengine.com/showthread.php?695-quot-Look-at-object-quot-event-in-Blueprint (which is a discussion about look at object, which is basically what you’re asking in the first place ). I would however work on tracing the spline of the track (assuming that’s how you did it, if not adapt as necessary) and in addition using the distance between checkpoints to actually drive the ‘wrong way’ BP so that it only triggers with motion in the wrong direction instead of just pointing that way (I’m still concerned if you just use a look at object event, if your track isn’t an oval like a nascar track, you’ll end up frequently triggering the ‘wrong way’ text when you’re on the track going the right direction, you’re just simply in a turn that has you pointed ‘the wrong way’ compared to where the next check point is, when really it’s just around some more turns.
I did this, but it doesn’t work:
So how is it not working?
Step through each part and confirm you are getting the results you expected, if you are - then explain why it’s not working, if you are not - what parts specifically are not working? Can you test with just that part and see if you can find the issue?
If you know how you want your BP flow to work, step through each part of your idea and confirm it works as you go, putting together an entire idea and then trying to figure out why it doesn’t work is rarely easy to do.
When I press W, I see this:
This is the Actor I’ve placed:
Why it never says “Wrong way!!” if I go on the wrong way?
This is a very simple but non-ideal and time-consuming solution.
Create new Blueprint named BP_WayPoint. The blueprint will have a variable of type Int wchis is public (with name Index). Within the compoent setup, add a spahre or box collsion component and Block to false, but make sure Generate Overlap is enabled. Inside you Charcter blueprint add a varible of type Int (named ActiveIndex).
During level design, you will place instances of BP_WayPoint along your rece-track and edit the Index variable from 0,1,2… ie The waypoint at start of the track will have index 0, the next one will have 1 and so on. This is the time-consuming part.
In the Character blueprints BeginPlay event, you will set ActiveIndex to 0.
In the BP_WayPoint blueprint’s event graph, add event for BeginOverlap. In this event, you check if the value Index variable of the overlapped WayPoint is greater than ActiveIndex. If it is, then you are going in the proper direction. Otherwsie you are going the wrong way. In either case, you will update the ActiveIndex with the Index of the overlapped WayPoint
You will need addiotnal logic to make sure the message is not shown when you are reversing (sicne the index will decrement in this case).
The accuracy of this method depnds on how well you place the WayPoint objects. In a strignt road, you only need one at the start and end. But at turns, you might need more WayPoints.
Actually you could reduce the level designers work (of setting indices) by making the WayPoint obejects hold a refrence to each other (like a linked list). But once you get this simple emthod working, you will be able to figure taht out yourselves.
Very simple logic.
No tracing so better performance.
You can make ti work in tight curves… by poerply placing the WayPoints.
Time consuming. In bigger tracks, setting all the indices is very tiring.
Will not work if the track has branches.
Hi. I am the 3D artist on this project. Our track will definately include braches/shortcuts therefore the suggestion kindly provided by mindfane probably won’t solve the issue in this specific case.
I have been experimenting with several techniques and the following one might work for you.
What you need.
- A blueprint BP_WayPoint extended from Actor with the following setup.
A public float variable named ‘Radius’
Event attached to ‘Begin Overlap’
- A blueprint extended from Pawn/Character with the following setup.
A variable named ‘Active Waypoint’ of type Object’BP_WayPoint_C’
A boolean variable named ‘bWrongWay’ with default value set to ‘false’
A function named ‘Set Active Waypoint’
Event attached to ‘Tick’
- A blueprint extended from HUD with following event graph to display Wrong Way text.
How does it work:
The BP_Waypoint blueprint must be placed along the track to indicate waypoints. The rotation of this Actor indicates the desired rotation of the car at that point (the arrow component will help with this). You dont have to be very precise. You need to place these actors wherever there is change in direction in the track. You can place them in branches as well. Edit the property of ‘Radius’ to set the correct radius for the collision sphere.
The Pawn has a reference to one instance of BP_Waypoint actor. This is set when Pawn overlaps with a Waypoint. We use the function SetActiveWaypoint function to do this.
At each tick, we find the difference between the rotation of the pawn and rotation of ActiveWaypoint. If the difference of Yaw is above a threshold (160 degrees in this example), we set bWrongWay to TRUE, otherwise FALSE.
In the HUD, we simply check the value of bWrongWay and display the warning.
This is a very basic setup. You must take velocity, reversing etc into consideration. This implementation only looks for the rotation of the Pawn. You can for instance set WrongWay only after Pawn has moved in the wrong direction for a minimum of X distance or Y seconds… Do this in tick and set bWrongWay accordingly. You can add additional code inside SetActiveWaypoint to optimize things and also to make more complex stuff.
This method works with branches as well. However just try to avoid overlaping waypoints.
It doesn’t work.
The pawn BluePrint:
The BP_Waypoint (I named it Checkpoint):
The Set Active Waypoint function:
The BP_Waypoint Components:
Solved. I used the trigonometric math. Thanks to iveytron.
Glad you got it figured out!!
However could you help me out if you don’t mind? Does this method work correctly when there are hair-pins in the track (ie 180 dgree turns). Does it show Wrong Way when you make the turn? I am very much interested in this topic. So I’m looking for different techniques.
Could you tell me what did not work exactly? You didn’t see any ‘Wrong Way’ messages on the top-left corner?
Anyway check these:
The Blueprint extended from Pawn must be the Player charcter (in you case this would mean you need to extend from Vehicle).
Make sure the HUD blueprint is the one beign used in GameMode
Check collsion settings and see if Begin Overlap is fired correctly.
You can set the Wrong Way with a simple check.
For example I set a Branch and I put a condition (the result of ACOSd < 45).
About your Blueprint, it doesn’t say anything.