- Home /
issue with AI shooting other AI
Link to Gap between the ray shots and the target when raycast variance is set to 0
https://www.dropbox.com/s/hogiq4wyojc7kdi/Gap.png?dl=0
Hey guys as the title states, i'm having an issue with my AI soldier shooting at my Zombie AI,
im using transform.LookAt(target) on my Soldier to look and shoot a raycast at my Zombie with a (-0.02,0.02) offset for that realistic aiming
these 2 functions pretty much make up the issue, the lookAt is looking somewhere off center of all my Zombie Ai, and when i use a LookAt their actual transform it is at the ground position, so to counter this i made an Aim GameObject and attached it to each of the AI and adjusted it accordingly, so when my Soldiers cycle through the zombies in the area they target the closest one and then set their lookat position to their child Aim location, but this did not seem to work at all, i'm still getting the error. is their a way i can fix this transform.LookAt to look at my target accordingly. i have also adjusted where the raycasts get shot from, i usually use a custom empty GameObject ShootFromPosition and put that at the Soldiers Weapon Barel position along with the weapon effects.
the image provided shows that it will hit sometimes, but at majority of the time it hits way off its target always to the right.
void LookAtTarget ()
{
//stop moving when looking at target!
agent.SetDestination(transform.position);
//this used to be that actual Targets position, but created a Aim location attatched to the targets to try fix the issue, but no luck with doing so.
transform.LookAt(aim.position);
}
void Attack ()
{
if (Time.time > attackSpeed) {
Effects ();
RaycastHit hit;
Vector3 direction = transform.TransformDirection (Vector3.forward);
//Weapon Spread
direction.x += Random.Range (-BulletSpreadFactor, BulletSpreadFactor);
direction.y += Random.Range (-BulletSpreadFactor, BulletSpreadFactor);
direction.z += Random.Range (-BulletSpreadFactor, BulletSpreadFactor);
if (Physics.Raycast (shootFrom.position, direction, out hit))
Debug.DrawRay (shootFrom.position, direction * 100, Color.blue, 5f);
if (hit.collider.tag == ("Enemy")) {
hit.collider.GetComponent<Health> ().curHp -= Damage;
//move to effects later
Instantiate (bloodSplat, hit.point, Quaternion.identity);
}
else if (hit.collider.name == "Head") {
hit.collider.transform.root.GetComponent<Health> ().curHp -= hit.collider.transform.root.GetComponent<Health> ().maxHp;
Instantiate (bloodSplat, hit.point, Quaternion.identity);
hit.collider.GetComponent<SphereCollider> ().enabled = false;
}
//random range this for some nice fire variences
attackSpeed = Time.time + Random.Range (nextAttack / 2, nextAttack * 2);
}
}
Your answer
Follow this Question
Related Questions
Simulating Mouse Input for AI 0 Answers
Visible GameObjects for AI 0 Answers
Follow up to AI Pathfinding Question 0 Answers
AI Look at X axis more efficient code 1 Answer
Need Help making Pong AI beatable 1 Answer