I finally finished writing the base code for my player’s combat system. However, I want to implement a Lock-on/Trigger system that will have the player facing the enemy and having him attack towards the enemy he is targeted to. I also want to display a ring or a targeting icon on the enemy that is targeted.
The player’s movement is based off the Lerpz ThirdPersonController.js and the player uses a CharacterController.
I speculate that something around the lines of using this
var rushDirection = -theTarget.transform.forward; //Move toward the enemy
GameObject.Find("Player").transform.eulerAngles.y = theTarget.transform.eulerAngles.y * 180; //Face the enemy in the direction the player is rushing
I figured out now to get the player to rush forward with his attacks by using this, I figured this area would be a good start to look at as far as making the enemy’s attacks move the player towards the enemy, rather that just forward:
//================================================================//
//Rushing Forward
//================================================================//
var smoothTime = 1.0;
function RushForward(rushDistance : Vector3)
{
var rushTime = Time.time;
while (rushTime+smoothTime > Time.time)
{
charController.Move(rushDistance*(rushTime+smoothTime-Time.time)/smoothTime*Time.deltaTime);
yield;
}
}
Here is a snippet of the attack system if you think it will help out any. I left only one attack in there to make the script a bit shorter
//ThirdPersonCharacterAttack
//Rewritten by SirVictory
//This script is only for the player's attack
var me : ThirdPersonStatus;
//====Basic Settings===================================//
var attackSpeed = 1;
var attackHitTime = 0.2;
var attackTime = 0.4;
var strength;
var liftForce = 1.0;
//===================================================//
//===================================================
/* -Making an attack-
=====================================================
In order to make an attack you will need the following variables:
var attackStrength = The knockback distance of the blow
var attackPosition = The origin of the attack sphere relative to the player's center
var attackRadius = The size of the attack sphere
var attackHitPoints = The anount of damage that the attack deals
*/
//=======Individual Attack Settings===================//
//Punching
var viewPunchHitBox = true;
var punchStrength = 1.0;
var punchRadius = 1.3;
var punchPosition = new Vector3 (0, 0, 0.8);
var punchHitPoints = 1;
var punchLiftForce = 2;
//=====Combo Timers================================//
var fireRate: float = 2;
var comboNum: float = 3;
private var fired: boolean = false;
private var timer: float = 0.0;
private var comboCount: float = 0.0;
private var lastPunchTime = 0.0;
//=================================================//
//======Flags======================================//
var groundAttacking = false;
var airAttacking = false;
var busy = false;
//================================================//
//=====Sounds====================================//
var punchSound : AudioClip;
//===============================================//
function Start ()
{
var me : ThirdPersonStatus = GetComponent(ThirdPersonStatus);
strength = me.attack;
//You can increase the speed of the attack animations by increasing the attack speed
animation["punch"].speed =attackSpeed;
animation["kick"].speed = attackSpeed;
}
function Update ()
{
var playerController : ThirdPersonController = GetComponent(ThirdPersonController);
//This section controls the actual attack attacking logic
//If attacking on the ground
if (!busy !playerController.IsControlledDescent() !playerController.jumping Input.GetButtonDown("Fire1")) {
if (!fired) {
fired = true;
timer = 0.0;
comboCount = 0.0;
Debug.Log("Punch!");
SendMessage ("DidPunch");
busy = true;
} else
{
comboCount++;
if (!busy comboCount == comboNum)
{
Debug.Log("Kick!");
SendMessage ("DidKick");
busy = true;
}
}
}
//If attacking in the air
if (!busy !playerController.IsControlledDescent() playerController.jumping !playerController.IsGrounded() Input.GetButtonDown("Fire1"))
{
if (!fired)
{
fired = true;
timer = 0.0;
comboCount = 0.0;
Debug.Log("Aerial Kick!");
SendMessage ("DidAirKick");
busy = true;
}
else
{
comboCount++;
if (!busy comboCount == comboNum)
{
Debug.Log("Aerial Kick!");
SendMessage ("DidAirKick");
busy = true;
}
}
}
if (fired) {
timer += Time.deltaTime;
if (timer > fireRate) {
fired = false;
}
}
}
//=============================================================//
//********************** LandAttacks *************************
//=============================================================//
//================================================================//
//Rushing Forward
//================================================================//
function RushForward(rushDistance : Vector3)
{
var rushTime = Time.time;
while (rushTime+smoothTime > Time.time)
{
charController.Move(rushDistance*(rushTime+smoothTime-Time.time)/smoothTime*Time.deltaTime);
yield;
}
}
//=============================================================//
//Standard Kick
//=============================================================//
function DidKick ()
{
var rushDirection = meTransform.transform.forward;
animation.Play ("kick");
//Lift up the player
LiftUp(10);
//Rush and Lift the player up
RushForward(rushDirection * 3);
yield WaitForSeconds(attackHitTime);
var pos = transform.TransformPoint(kickPosition);
var enemies : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
for (var go : GameObject in enemies)
{
var enemy = go.GetComponent(EnemyDamage);
if (enemy == null)
continue;
if (Vector3.Distance(enemy.transform.position, pos) < kickRadius)
{
strength = kickStrength;
liftForce = kickLiftForce;
enemy.SendMessage("ApplyDamage", kickHitPoints);
// Play sound.
if (punchSound)
audio.PlayOneShot(punchSound);
}
}
yield WaitForSeconds(attackTime - attackHitTime);
busy = false;
//groundAttacking = false;
}
@script RequireComponent(AudioSource)