I’m making an rts game and i’m having a little trouble with the player mechanics.
I’m using unity navmesh for movement with ray-casting and so far its working grate.
so far I have goten the enemy AI working perfectly were the enemy choses a random target using an array then follows and attacks when the distance is close enough.
I’m able to move my player ships around the map by left clicking to select then right clicking to move to a spot on the navmesh (this part works fine) the problem is that I’m wanting to right click on an enemy, and the player ship that I’ve selected will follow the enemy and when he’s close enough he will attack.
I have looked at my code so much that its hard to see now lol.
I was wondering if anyone could point me in the right direction.
the player ship locks on to the game object that I have loaded into a variable by ray-casting and tag finding but doesn’t follow. it loads properly but the player ship ends up going to the place that the enemy was.(the object does load into the variable properly)
and when I want the ship to auto fire it only does by Onmouse down ( I know I have it in the Onmouse down
if-statement but if I put it else were it doesn’t work at all. also the player ship doesn’t tern towards the enemy when firing either.
sorry if my explanation is a little to vague.
heres my code . if any one can help thanks and also sorry I know my spellings bad.
#pragma strict
//auto attack/track if within distance
var object;
var distance : float = 10.0;
//controls state changes
var sw : int = 0;
//Navagation mesh components
var navComponent : NavMeshAgent;
//HPBAR
var playerHpBar : GUITexture;
var phpcurrent : float = 100.0;
var phpmax : float = 100.0;
var percent : int = 20;
var dam : int = 10;
//efects for attack and destroy stuff
var BeemAttackPrefab : GameObject;
var Beem : AudioClip;
var Expl : GameObject;
//attack timer controll
var attackTimer : float;
var coolDown : float;
//raycasting
var hit1 : RaycastHit;
function Start () {
navComponent = this.transform.GetComponent(NavMeshAgent);
}
function Update () {
//HPBar controll
var percentofhp = phpcurrent/phpmax;
var hpBarLength = percentofhp*percent;
playerHpBar.guiTexture.pixelInset.width = hpBarLength;
if(phpcurrent <= 0)
{
phpcurrent = 0;
Explosion();
}
//Attack cool down controle
if(attackTimer > 0)
attackTimer -= Time.deltaTime;
if(attackTimer < 0)
{
attackTimer = 0;
}
switch(sw)
{
case 1:
if(Input.GetMouseButtonDown(0))
{
sw = 0;
}
if(Input.GetMouseButtonDown(1))
{
var ray1 = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit1 : RaycastHit;
if(Physics.Raycast(ray1, hit1, 100))
if(hit1.collider.tag == "en")
{
var object = hit1.transform.gameObject;
distance = Vector3.Distance(hit1.transform.position, transform.position);
var dir : Vector3 = (hit1.transform.position - transform.position).normalized;
var direction = Vector3.Dot (dir,transform.forward);
print ("object hit!");
if(distance <= 5.0) // && direction > 0.8)
{
sw=2;
}
else
{
navComponent.enabled = true;
navComponent.SetDestination(hit1.transform.position);
}
}
else
{
navComponent.enabled = true;
navComponent.SetDestination(hit1.point);
}
}
break;
case 2:
if(distance <= 5.0)// && direction > 0.8)
{
navComponent.enabled = true;
transform.LookAt(hit1.transform);
if(attackTimer == 0)
{
attackTimer = coolDown;
attackFire ();
}
}
else
{
navComponent.SetDestination(hit1.transform.position);
//if(Input.GetMouseButtonDown(1))
//{
// sw = 1;
}
break;
}
}
function OnTriggerEnter(other:Collider)
{
if(other.tag == "Beem2")
{
this.phpcurrent -= dam;
Destroy(other.gameObject);
}
}
function OnMouseUp (){
sw = 1;
}
function attackFire () {
transform.LookAt(hit1.transform);
Instantiate(BeemAttackPrefab,this.transform.position,this.transform.rotation);
audio.PlayOneShot(Beem);
}
function Explosion (){
var Explo : GameObject = Instantiate(Expl, transform.position, transform.rotation);
Destroy (this.gameObject);
}
@script RequireComponent(AudioSource)