Labels

Thursday 5 December 2013

Mechanic Movement Updates

Now that the main characters motor script is in the process of being finalized I have begun to make a start on scripting the mechanics for our controllable companion the "Mechanic". So far this has only involved scripting the Mechanics movement as this is required to be able to get any other features working.

As with the main characters motor script I have been through two attempts to get the script up and running. The first iteration of this was a incredibly simple script that used a set of boundaries to keep the Mechanic following the player when they are moving combined with the use of the mouses position to control the mechanic once you switch to it. Bellow is a snippet of this script that demonstrates how this basic movement worked.


However this had be scrapped as firstly the boundary system that was used to make the mechanic follow the player gave unpredictable movement that appeared jittery on screen. Secondly as we have now made the decision to make the game controller compatible, the idea of using the mouse to control the mechanic became obsolete.

I then re-wrote the script and instead of using the mouse position to control movement, I recycled some of the main characters movement code to base the new system off the same key presses as main characters movement. It is also based off the same acceleration system with some slight alterations to make the movement better represent that the Mechanic is floating.

The main method that was used to achieve this affect was to incorporate a sine wave to all of the movement made by the Mechanic. This sine wave gives the appearance of floating as the Mechanic will now slightly move up and down smoothly as you move.

This code snippet shows how the sine wave was achieved:



 While the updated movement script was based off the working motor script of the main character some aspects had to changed as the character motor script controls a character controller where as the Mechanic is a rigidbody. The other main issue with this attempt was that the Mechanic was being moved by manipulating its transform. The reason for this being an issue is that when you move something via its transform property the object is actually being teleported between different locations on the screen, which results in the affected rigidbody passing through objects with coliders attached.

This problem was eventually fixed by using an in-built move function that draws a line between its current and target positions and will make the rigidbody trace that line 1 point at a time. Which also allows for collisions as the rigidbody can no longer be teleported inside of an objects collider. This function combined with some tweaking of the rigidbodies settings gave the original desired movement.

Below is a snippet of code that shows how the script for this is set out.


Also the issue with the jittery movement when the Mechanic was following the player was fixed by removing the boundaries and have the mechanic constantly lerping to the position of a game object that I childed to the main character in the location that we wanted the mechanic to be.