Right way of handling mouse input - unity3d

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.

Related

How to modify behaviour of a VR controller's pointer

My university colleagues and I are trying to develop a Virtual Reality project for university where we use an Oculus headset and create a scene with a mouse where you can select and click and drag different objects in the scene. You are supposed to be stationary and move one of the controllers as if it was a mouse. However, we want to modify the behaviour of the controller to better fit the 3D environment. When an object is not selected, we want to interpolate the depth of the cursor according to the interpolation of the nearest objects. There is a paper that we were shown in class that we are supposed to drag inspiration from, and it achieved this kind of behaviour of the cursor with a normal mouse but I can't seem to find any information on how they did it. Our final goal would be to compare both ways of managing the scene and assess which one is better. We are using Unity with VRTK as suggested by our professor, but we and can't really seem to be able to access the mouse's file on how it moves or its behaviour, and we are kind of lost on where to go. Could someone help with this?
Here is the paper where they talk about it:
https://dl.acm.org/doi/pdf/10.1145/3491102.3501884
We so far have tried creating a simple scene and adding objects with different behaviours as well as a controller instance, but we seem to only be able to modify the events of the mouse and not its specific behaviour.
Kind regards and thanks

Unity C# collision detection

I made/trying to make a melee two-dimensional game in Unity. The enemy can attack the player no problem, but the other way around is very finicky and unreliable. The enemy has both a ridged body and a box collider, and the same with the player.
This code is all on the enemy
code: https://pastebin.pl/view/a6d99e0d
Because I do not know the specific project, I can not give a specific answer.
But I can provide a similar dilemma I have encountered before. My input was not responded in time in the game. When the player pressed after the jump key, the protagonist may not have been be able to jump in time. When I changed the FixedUpdate in the code to Update, the problem was solved.
Of course, my approach is flawed, but I hope my problem can be addressed to the subject. Bring some tips and good luck!
You're validating input, and updating physics both in the void OnTriggerEnter method. Physics updates much differently than the regular old Update() method does.
Your physics checks are already completed (I guess in this case it could be either or).
By calling Input.GetButtonDown() in this method, it has to grab the exact frame that button mashed down to return true.
The problem is, physics don't Update in the same way. Actually, you aren't even running a physics check this frame. That button just got completely ignored. Dang.
You have to come up with a better way to structure your code to process that input. Find a way that allows you check if the player is attacking, without relying on input. You could always try a state pattern.
See also MonoBehaviour.FixedUpdate().

VR: How to form hands around object they are holding in Unreal?

I'm working on a VR project in Unreal and I'd like my player hands to form to certain objects whenever the user grabs them. (I.e. the way our hands work) Unfortunately, I haven't really found any examples online of others doing this.
For example, I'd like when the user picks up say a hand held tool like a hammer that the hand would wrap around the handle. When the user grabs a basketball the hand shouldn't be closed but, expanded like a you would if you were to palm a basketball in real life.
I haven't done a lot of testing with this but, I'm pretty sure since the hands are based off of a Animation Blueprint that they simply ignore collisions and follow the animation.
I guess the simplest solution would probably be based off of collision where the hand plays an animation and as the fingers of the hand wrap around the object they stop at that position where they collide with the object in question. If it is even possible that is.

Handle pan, zoom and click with both Mouse/Touch on Unity3D

I'm working on a 2D turn based game and I'm in the condition to handle mouse and touch.
The game has an hexagonal map and requires pan, zoom and click action.
I decided to apply delegate-pattern so each object that requires an action, sends the event to its delegate who will do or not stuff.
In this way all inputs converge to a TurnManager that, with a state machine, handles events in accordance to the current game state.
Example:
MapCell.OnMouseUpAsButton() calls
delegate.OnCellClick(MapCell)
All works well and in this way I can handle when do something.
The problems arrived when I started to implement Zoom and Pan in the map.
For these two actions, I had to avoid classic Monobehaviour method (OnMouseDown, OnMouseUpAsButton, ...) and use LateUpdate.
So,
I created a CameraHandler that in the LateUpdate uses:
HandleMouse()
HandleTouch()
and, using delegate pattern, evokes the actions below:
OnMapWillPan()
OnMapPan()
OnMapEnd()
To avoid Pan or Clicks over UI elements, TurnManager filters received events with EventSystem.current.IsPointerOverGameObject()
Problem
On Mac/Mouse all works great! :D
On smartphone/Touch I can't click on nothing and only Pan is working. The debug on device is infernal because the lack of breakpoint or console.
Questions
Do you ever handle this things? How?
Which approach did you use?
What do you think I'm doing wrong?
Are there best practices to avoid problem like this and handle correctly crossplatform input?
Are there any good lecture/book for this argument?
PS: if needed I can show the code

Mouse cursor becomes locked during keyboard input

I'm attempting my first shot at using Unity. This is my first foray into game development and 3D environments. I'm following the tutorial for the survival shooter located here: https://unity3d.com/learn/tutorials/projects/survival-shooter/player-character?playlist=17144
I went to try mouse movement, which should change the direction of the player object, but found that the player does not change direction.
I have even tried copying the tutorial author's code directly, using it in its entirety and overwriting my original script. Their code can be found in the link.
Adding Debug.Log("Raycast not hitting"); to an else block in the Turning function causes the debug message to fire during each FixedUpdate, regardless of whether the mouse has been positioned over the floor.
Since you directly copied all scripts, I'm assuming that their are no issues with your scripts. This means that this issue is most likely located somewhere in your scene. The boolean controlling player, Physics.Raycast(camRay, out floorHit, camRayLength, floorMask). The else statement you used assures you that this function is returning false. I think the most likely reason this may be is that you do not have an object in the scene that has its layer set to floor. The aforementioned function will automatically return false if the RayCast does not find an object with a layer of floor within a range of camRayLengthunits. So, find or create an object that covers the entirety of the floor and set its layer to floor. I would also recommend looking at the scripting API documentation for Physics.Raycast so that you can better understand whats going on in the code. Here's a link to Unity's documentation: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html