There were many parts to be created for the shark assest and I was tasked with the coding part of it. The shark went through a lot of stages. The first was the development of a follow code to make the shark follow and then attack the player. I took a basic script made by James (a group member) and developed it further. I then made use of the new Unity UI and created a texturespawn script that would be called by the attack of the shark. This was done by just getting the script within the SharkMovement start function and then calling it on each attack.
SharkMovement(and attack) Code
SharkAttackTexture Code
However you might be wondering upon evaluating the first script about how this script is actually called. The answer to that is simply a statemachine that controls the mode the shark resides in. Its kept in a patrol script when the player is outside of its trigger range but upon trigger entry changes to the sharkmovement code.
Shark Controller
The patrol script took a lot of work to produce correctly. I had to learn about arrays and how to use them to create reference objects. By assigning 3 of these reference objects as waypoints and adding a box collider trigger to all 3 so they act as a checkpoint for the patrol script. Basically the waypoint count starts at 0 and each waypoint has a reference of either 0, 1, or 2. Upon reach the current transform position, checked by trigger entry, the waypoint reference is increased by one. When reaching the last waypoint, 2, the reference is reset back to 0 to continue the patrol loop.
Patrol Code
These sets of code were not too hard looking back. The only issue really was understanding how to use arrays correctly and once understood allowed waypoints to be created easily. Making the shark follow these waypoints was a bit annoying but upon adding ridigbodys and the like I realized my mistake. Essentially a trigger input needs a rigidbody.
No comments:
Post a Comment