- Home /
My Boss wont work
Hello All, I am writing a game application and I seem to be having some issues with my Boss. Here are the top 3 things wrong with it:
1- The boss doesnt chase the player. basically, every time the player gets withing range te boss doesnt chase him. This is what I got:
function SearchPlayer (position : Vector3) { // Run towards the player but after 3 seconds timeout and go back to Patroling var timeout = 3.0; while (timeout > 0.0) { MoveTowards(position);
// We found the player
if (CanSeeTarget ())
return;
timeout -= Time.deltaTime;
yield;
}
} function CanSeeTarget () : boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false;
var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;
return false;
}
2 - The boss orientation with the player moves to the side every time it shoots so it never gets to the player. This is the code I got:
function Shoot () { // Start shoot animation animation.CrossFade("shoot", 0.3);
// Wait until half the animation has played
yield WaitForSeconds(delayShootTime);
// Fire gun
BroadcastMessage("Fire");
// Wait for the rest of the animation to finish
yield WaitForSeconds(animation["shoot"].length - delayShootTime);
}
function AttackPlayer () { var lastVisiblePlayerPosition = target.position; while (true) { if (CanSeeTarget ()) { // Target is dead - stop hunting if (target == null) return;
// Target is too far away - give up
var distance = Vector3.Distance(transform.position, target.position);
if (distance > shootRange * 3)
return;
lastVisiblePlayerPosition = target.position;
if (distance > dontComeCloserRange)
MoveTowards (lastVisiblePlayerPosition);
else
RotateTowards(lastVisiblePlayerPosition);
var forward = transform.TransformDirection(Vector3.forward);
var targetDirection = lastVisiblePlayerPosition - transform.position;
targetDirection.y = 0;
var angle = Vector3.Angle(targetDirection, forward);
// Start shooting if close and play is in sight
if (distance < shootRange && angle < shootAngle)
yield StartCoroutine("Shoot");
} else {
yield StartCoroutine("SearchPlayer", lastVisiblePlayerPosition);
// Player not visible anymore - stop attacking
if (!CanSeeTarget ())
return;
}
yield;
}
} function RotateTowards (position : Vector3) { SendMessage("SetSpeed", 0.0);
var direction = position - transform.position;
direction.y = 0;
if (direction.magnitude < 0.1)
return;
// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime);
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
}
3 - There I a feature I wanted to add to the boss called a force field which would show up every time the player shoots at it while the shield hit points are still greater than 0. But the things is that every time it shows up it appears next to the boss and not overing te boss. Tis is the code I got:
var Shield : GameObject; var shieldPoint = 100; var shieldMaxPoint = 100; var ShieldPosition : Vector3;
function Start() { Shield.renderer.enabled = false; } function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.tag == "Energy Bullet")
{
if(shieldPoint > 0)
{
Shield.transform.position = GameObject.Find("Boss").transform.position;
Shield.renderer.enabled = true;
if(Shield.renderer.enabled)
shieldPoint -= 5;
}
hitPoints -= damage;
}
if (hitPoints <= 0.0)
{
Detonate();
isDead = true;
}
}
I hope to hear responses from you awesome users and thank you so much for your help!
Can you please reformat your code properly? It would be a lot easier to read.
Just a thought on problem 1 - is your physics line cast hitting the boss first and therefore always returning false when comparing the hit item to the target? You could fix that with some layer masking.
To reformat your code - select it and click the code (binary numbers) icon - or add 4 spaces before it on the line. I can't really make head or tail of it like it is.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071748im_/https://answers.unity.com/themes/thub/images/avi.jpg)