Interaction with object by pressing E button using ue4 blueprints - unreal-engine4

I am a begginer in ue4 blueprints system. I've wanted to create simple code: player enter the trigger box, and press E, after that text should appear on the screen. What I did wrong in my code?

I'm no expert, but I think you should use a branch node to check if the key is pressed. No harm in also changing your other character to a getplacyercharater.
Best of luck :)

On Component Begin Overlap (Box) fires on the frame you begin overlapping. Unless you pressed E on the same frame you begin overlapping, this will not work.
For this to work, you would have had to be checking if it was overlapping and for the key press on every tick. Don't do that, it's a waste of time.
What you could do instead is On Component Begin Overlap, set a variable to record that you're ready to interact, and then check for that variable when you press E.
This is something I did recently using the Top Down template that demonstrates 'move within distance of object, press button, thing happen'
Adding an object to a 'ready to interact' array on overlap in Player Character.
Detecting a mouse click, checking if it was in the array, doing stuff in Player Controller.
It sounds like you're building an interaction system though, so if you want this to be as re-usable as possible you should probably have this code on your Player Character / Player Controller, and not in the object you're interacting with.

Related

Moving mouse cursor with gamepad question

So I can create a gameobject that I can move around like a cursor with my xbox controller, however when it comes to pressing buttons I have 1 issue.
It seems there is no way to set the actual mouse cursor position without importing user32.dll and even that solution will only be a windows solution. https://answers.unity.com/questions/330661/setting-the-mouse-position-to-specific-coordinates.html
The reason I want to move the actual mouse cursor as well as my gameobject representing a cursor, is that I still want to be able to click buttons, and when I call Input.GetMouseButtonDown(0); it will click the button at the point where the actual mouse cursor is, and if the actual mouse cursor isnt moving with us its not gonna work.
Also I dont need a custom mouse cursor, if the xbox controller could move the mouse cursor directly thats fine too.
Any advice appreciated.
There is no other way to set the mouse position maybe if you needed to have a solution for mac as well you can find it here at the bottom.
also making a custom cursor is a better solution cause it will be cross platform and you could make a custom mouse down event for it so i don't see why you wouldn't
and if you are sure you need the actual mouse to move you only have the first solution
and moving the mouse with xbox controller by using a software is only gonna work on your pc and those softwares are probably using the first solution

how to reference an "armature action" of a bow using code?

I'm trying to make a bow and arrow game. I downloaded a bow that has an animation(?) on it where the string is pulled back.
See:
I want to make a script that triggers that animation (shown on bottom-right of my gif) when Player left clicks. But I don't know how to reference the animation.
Is it something like _anim = GetComponent<Animator>(); and then animation.Play? I cannot tell what the name of the animation even is to do this.
Further.. it would be awesome to be able to control the animation's length depending on how long the user has held down the button, rather than playing it in full even if user only taps left click. I'm not sure how that would be achieved?
Ok, the question seems short but the solution will be long and you need to do a bit of learning. So, let's go through your question step by step.
I downloaded a bow that has an animation(?)
Yes, it is a 3D bow model with an animation in it.
I want to make a script that triggers that animation (shown on
bottom-right of my gif) when Player left clicks. But I don't know
how to reference the animation.
In order to trigger that animation, you need an Animator Controller attached to the bow model instance. Then you can click that Animator Controller to open its panel. After opening the panel, you can simply drag and drop your animation there. When you first drop your animation, it will be the default animation state. However, if you don't want to trigger the animation immediately, I would suggest you to create an empty state and make a transition to the bow animation.
Is it something like _anim = GetComponent(); and then
animation.Play?
More or less like this, but you should check the documentation for better understanding, this documentation explains it well and has a really good example for your use case.
I cannot tell what the name of the animation even is to do this.
Once you drag and drop the animation to the panel, you will see the name of the animation to use in the script.
Further.. it would be awesome to be able to control the animation's
length depending on how long the user has held down the button,
rather than playing it in full even if user only taps left click.
I'm not sure how that would be achieved?
There are many ways to play with animations, for example, you can set an animation parameter to stay in the animation state. Furthermore, you can also disable the Has Exit Time value from the transition to make it stop the animation immediately after the user stops holding the button.
Overall, when it comes to animations, Unity is quite powerful and my suggestion would be read the documentation and watch a couple of tutorials.
Hope this helps.

Allowing click or raycast to only certain items at time

This is kind of hard to explain so I try to do my best using the following image.
So the case is this: player has selected drill from inventory and "Confirmation menu" (A, S & D) has popped up. Now the player must select one of these actions (A = accept position, S = put item back to inventory, D = keep moving object around). Before the player chooses from one of these action he should not be able to do NOTHING else.
The problem is that if the player clicks the minimap in the upper right corner a button click is detected and menu is opened. If the player clicks the car (detected with Raycast) player gets inside. If the player drags the joysticks he starts walking around (using Unity's Joystick script). NONE of these should be allowed.
To the question: does Unity have some kind of method to "Prevent any kind of click detection to any other objects but these" OR "If mouse is not over this object do nothing when clicked"? There probably is nothing like that so what would be the best workaround?
Because these "click to do something" actions are triggered from various places with various styles (buttons and raycasts in tens of different scripts) I try to avoid doing this by setting booleans everywhere. Open ideas are more than welcome.
An easy workaround could be via using GUI Depth (https://docs.unity3d.com/ScriptReference/GUI-depth.html).
If "Confirmation menu" guiDepth is on top and all the other GUIs are below, you could add another GUI in between covering the whole screen; a Raycast would then return the first element and if it's the middle GUI just do nothing.

Right way of handling mouse input

I was playing around with navmesh agent and im pretty happy about the results i get. But i am a liitle bit concerned about the code getting complicated.
I want to organize my code in a way that allows me to edit it later without trying to figure it out what i did there.
what i need is basically is this:
Handle mouse click on ground, enemies, objects, skill / spell targeting, gui
Handle mouse over objects, enemies, gui
my approach was:
in update function raycast mouse position
check if the mouse was clicked
if clicked check the target tag : enemy, ground, object (loot) and
call a related function
if not clicked check the target tag again for hover effects.
so what would be the best way to handle all of these listed above? any code examples in any language would be appreciated.
thanks for your time
its been a while that i asked this question. i ended up using state design pattern explained here.

Switch turns between the Player and AI in Unity

I've recently started using Unity for a resource management stealth game. The stealth part is turn based, similar to Hitman Go. I have a simple character controller and a simple patrolling AI over a specific path. However, these movements work in real time and I want to change that to turn based. The AI should wait for the player to finish his/her move and then move itself. The same goes for the player.
Both the player and AI should be able to move to their adjacent waypoints only when the movement of the other part is complete.
How should I go about that?
Thank you
The language that I'm writing in is UnityScript.
As a very simple solution, firstly you can create an empty gameobject. Name it as TurnController. With a simple script you can add a boolean variable on it. Lets name it as isPlayerTurn. For player movement you can check this, if it is true player can move. At the end of his/her move (maybe clicking end turn button or when it reachs the max distance to move or something else) you can set isPlayerTurn false. Ofcourse AI should check (Maybe in Update function. But can change by your design) if it is true, AI can do what it needs to do. And at the finish of its turn, it should change isPlayerTurn back to true. I know it is a very simple solution but hope it helps for begining. And I hope I didnt misunderstand your question.
Write the ai as a player instance and have it emulate player input.
(Instead you could also implement a common interface on both classes.)
Spawn a game object with a GameManager behaviour script that stores a reference to the current player (or ai). Then have the GameManager update the current player every frame by checking their input. If the (human) player gives input while it is not his turn, his input will just be ignored.
This way, the player and ai do not have to know if it is their turn.