In the last blogpost I wrote about cleaning the Demon class and creating a FSM to make it easier to implement new states. Now I have been able to harvest what I sow. Now the Demon player can sling objects against the Human player to inflict damage. As a start I made a prototype on the sling in one of our offline test scenes beffore the implementation to the real game. This was mostly so that I wouldn’t need to bother with network stuff and other things which could have an impact.
At the beginning I created a new prefab and added a RigidBody and two BoxColliders to it. The RigidBody is for the movement of the object and also for the physics. Now the prefab will have a more realistic movement when the player slings it away and will be able to bounce of the walls. One of the BoxColliders are for the object to collide with objects so it doesn’t fall of the map. The other collider is set to ”as trigger” so it will be able to recieve signals from the mouse as well as the objects it collides with. Now when the object got these properties I can use the OnMouseDown function, which is called when an user clicks on an ”as trigger” collider. The object will then wait until the mouse button has been released beffore it starts to calculate the distance between itself and the mouse position, and then set a velocity equal to that distance.
When the prototype was done and worked, as it was itended to do, I started to create a new state for the Demon which is called Sling. The Sling state is called when the Demon player has clicked on a special type of rock, which got the previous script on it. The state calculates the distance between the mouse and the rock when the mouse button has been relaesed and then sends a speed to the script on the rock. It also checks, every frame, the distance between the mouse and the rock and then stretch an image inbetween the two to show how long the distance is. The picture bellow is when you drag the rock. The only problem with the Sling mechanic is that the rocks will disappear when you sling them to hard. The reason is as with the Human from one of the previous blogs: RigidBodies ignore collision physics when the velocity is too high.