Greetings, it is time for blog posts again. This time it is for big game project. This course is about making a game in about 8 weeks, and then showing the game at the GGC conference. The game we are making is named Tale, and is developed in Unity.
This is the third week into big game project, thought the blogging starts now. Therefore I will describe the most important discoveries, solutions and problems related to code I have encountered during these three weeks.
The main things I have coded during the first weeks are:
- A state-machine for AI
- Simple AI behavior
- Rope
- Rope swinging
- Rope(physics based)
- Rope(climbing, descending)
State-machine:
For the state machine I tried following this tutorial. I followed this tutorial since it seemed to keep the code rather clean even if the AI would be expanded with more functionality and logic later.
The tutorial uses something called interface. An interface is like making a virtual void class in C++ except that this is in Unity(C#). This means that a class that inherits the interface class needs to have the methods the interface class has. Therefore you can safely assume that all classes that inherits the interface class has the same methods.
This enables me to make a class that holds a currentState, of the interface class type. And can change the currentState, to any other class that inherits the interface. In my case these states are(for now) alertState, detectedState, patrolState, retreatState and attackState.
So the class that holds currentState can call currentState.Update() in its own UpdateStates() function. This means that if i change currentState, the update function that will be called is the one that i changed currentState into.
For a more refined explanation check the tutorial.
Simple AI:
With the State machine that I made above I managed to create an enemy that patrols, chases, retreats, reloads, fire towards the player and has an attack cool-down. Though making a state for reloading, firing and attack cool-down was redundant since it will be handled by animations later.
FOV(field of view):
The enemy searches for the player with a sphere trigger. Once the player enters this trigger it checks the angle between the player and the enemy and compares it to a desired FOV.
If the player is inside of the FOV, the enemy fires a raycast to see if no obstacle is in the way of the enemy for seeing the player. If nothing is in the way, it means that the enemy sees the player.
Rope and more ropes:
The main focus for me these couple of weeks and coming weeks has been concerning making ropes. This is because we want a high variety of functionality of the rope. As a player you will be able to the following with a rope. Interact with enemies, swing, increase/decrease tether length, climb/descend, pull objects and probably something more.
Physics based rope:
The first attempt at making a rope I used Unitys Character Joints. These enabled me to connect multiple colliders and bodies into a long rope and render a line between them. Visually this works fine but mechanically it breaks.
Why it did not work:
When I tried to interact with the rope by adding force to a joint, or when the rope fell. It in the most cases started to freak out(literary). I assume the thing that happened was that when one joint was too far away from another they tried to snap, but when the snap happened the joint on he other side also tried to snap causing a ricochet of movement. This made so that the thing that was supposed to be a rope suddenly was a massive hurricane of lines. (Apparently Unity 5 has problems with physics calculations, and many forums suggests Unity 4.6X for physics games in Unity). Thought I did not try to downgrade from Unity 5.X.
Non physics based:
The solution so far is making it non-physics based. The biggest problem with the physics rope was force and high velocities therefore I wanted to make the swinging first with the non-physics rope. A great help on the way was this pseudo-code and explanation.
In short the way it works is that the object that is tether is restricted by a certain length. If the object would go outside of the radius it snaps back into range. At the start of each frame i save the objects position and in the end of the frame i save the position it snapped back to. Then by subtracting the two at the start of the frame enables me to set which velocity the object should have so that it follows around an object it is tether to according to the length of the rope.
(this code is under construction, that is why it is such a mess!)
For testing purposes I tried to implement this in 2D in a personal project before 3D.
Since the 2D code worked just fine, I translated it into 3D.
This is the result of the swing so far.
The climbing and descending I got working for the physics based rope, thought since I went for the non-physics I will probably rewrite it.
This was the functionality for the rope climbing on the physics rope from the first two-three days. Looks more like an rail-cart, since the rope is on the ground.
This was this weeks post, the next one will probably be shorter. Take care.