Saturday 8 September 2012

Locked-On


As the development of the Nightside Chronicles demo progresses, one of the newer features, the ability to lock-on to the nearest enemy, was successfully implemented in the latest test build of the game.

By holding the lock-on button, the player will automatically face the nearest enemy and a triangular pointer will appear above the enemy to indicate which enemy is currently being targeted. While locked on to an enemy, the player will be able to sidestep left & right and move forward & backwards as normal, all while continuously facing the targeted enemy. 


How it works: 

When the player presses the lock-on button, the newly created "lock-on" function in the PlayerCharacter script will search for all game objects nearby in the level that are tagged as being an enemy. A temporary array is then created to store references to each of these enemies and then a for loop is then used to cycle through the array, calculate the distance to each object and store a reference to the enemy where the distance to the player is the lowest out of all of the enemies stored in the array.

The world position of the enemy is then read from the transform component and this is used to create another vector variable called lookTarget, which will be where the player will be looking at. The lookTarget is essentially the same as the position of the enemy found to be the closest in the array, but the y value is set to be the same as the y value of the player's position to ensure the player model will face forward and not be tilted up or down due to variations in the y axis when the player object is set to look at the target.

Next, to actually make the player object face the enemy, first a new set of if statements is added to the movement/input section of the script so that when the lock on button is being pressed, the player won't be able to rotate when they move the left analog stick in the x or horizontal axis (as they would be able to ususally). Then, using the transform.LookAt() function, the player object is set to look at the position stored in the lookTarget variable, which will rotate the player to be facing the targeted enemy.

To help indicate which enemy is currently being targeted, the function also creates an instance of an upside-down pyramid and then positions it above the enemy by using the vector3 position in the lookTarget variable and adding 2 units to the Y axis.




No comments:

Post a Comment

Note: only a member of this blog may post a comment.