Hello Unity Community!
I’m hoping you guys can help me out with a script I’m working on. Basically, it’s a top down, 2D movement system (Click to move) that I want to tie into object interaction. This is one of the first scripts I’m working with on this project, and already am running into major trouble. Granted, my scripting background is sparse at best, so I’m hoping some of your more experienced individuals can help me out!
Objective of script:
Player clicks on the screen and grabs a location. This is done using a Raycast function that returns the object to a RaycastHit variable. Depending on a few variables, a series of interactions fires off at this point:
If the object is terrain, or unmarked (road, titled gradient,etc), then move the character directly from current location to target location (hit.point, etc) within a set distance (using a Vector3.Distance check of a small amount. The character (a cube in this case) needs to rotate to face the object before moving, or within short distance of starting. If the object it clicks on returns a “Harvestable” or “Container” tag, then the character moves to the object within a distance, and then executes the code to open the container or harvest the harvestable (not even thinking of these scripts at this point, but getting the placeholders set). If the object returns an “Enemy” tag, then move to the charcters weaponRange variable, and then execute cWeaponAttack script (basic scripts that would be written at a later date to do some math functions). If the object returns NPC, the character would move to the location, then when arriving, execute an openDialogue function (again, to be written later).
At this point, that’s all I want it to do.
TLDR:
Looking for help making a script. My attempts so far have not worked, so my framework is pretty much void at this point. Looking for a script to do:
Player = primitive Cube
Player clicks on Terrain → Cube rotates and moves to location of the raycast intersect.
Player clicks on Object/Harvestable → Cube rotates and moves near to the object, then fires off a script
Player clicks on Enemy → Cube rotates and moves within range of a stored weaponRange variable, then fires off a script
Player clicks on NPC → Cube rotates and moves to target NPC, then fires off a script.
Not looking for help (right now, heh) on any of the sub-scripts; just the movement script. All my attempts thus far have not worked properly (Cube doesn’t rotate/Cube only moves when clicking/Cube spins in place/etc, etc).
Working with a basic Terrain, standard cube, and the associated colliders. I’d post what I have but frankly, there are so many “let me see if this works” changes that aren’t documented that it will most likely have to be scrapped. All it does so far is make a ray in Camera.main.ScreenPointToRay(Input.mousePosition), then make a RaycastHit ‘hit’, does the basic Physics.RaycastHit check, then compares the tags and moves from there. I had the script storing a boolean isMoving that would then loop in Update on the transform.translates of a dir I made from transform.position - hit.position normalized and such… but nothing at all has worked.
Thanks in advance and I’ll do my best to return any more required info.