- Home /
Turret AI doesnt hit enemy
Hey guys, im currently working on a MOBA game like League of Legends but the AI Turrets are giving me a hard time. So far the turret detects the enemy box, creates a muzzle effect when a ball is fired but the ball doesnt hit the box, it goes way on top of the target. Here's a screenshot: ![Turret][1] [1]: /storage/temp/1793-turretai.jpg
Firebal Code:
#pragma strict
var mySpeed:float = 10;
var myRange:float = 10;
private var myDist:float;
function Update ()
{
transform.Translate(Vector3.forward * Time.deltaTime * mySpeed);
myDist += Time.deltaTime * mySpeed;
if(myDist >= myRange)
Destroy(this.gameObject);
}
Turret Code:
#pragma strict
var myProjectile : GameObject;
var reloadTime : float = 1f;
var turnSpeed : float = 5f;
var firePauseTime : float = .25f;
var muzzleEffect : GameObject;
var errorAmount : float = .001;
var myTarget : Transform;
var muzzlePositions : Transform[];
var turretBall : Transform;
private var nextFireTime : float;
private var nextMoveTime : float;
private var desiredRotation : Quaternion;
private var aimError : float;
function Start () {
}
function Update ()
{
if(myTarget)
{
if(Time.time >= nextMoveTime)
{
//Continuar a obter a posiçao do inimigo
CalculateAimPosition(myTarget.position);
turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * turnSpeed);
}
if(Time.time >= nextFireTime)
{
FireProjectile();
}
}
}
// Quando um inimigo entra no range da tower
function OnTriggerEnter(other : Collider)
{
if(other.gameObject.tag == "Enemy")
{
nextFireTime = Time.time+(reloadTime*.5);
myTarget = other.gameObject.transform;
}
}
// Quando um inimigo sai do range da tower
function OnTriggerExit(other : Collider)
{
if(other.gameObject.transform == myTarget)
{
myTarget = null;
}
}
function CalculateAimPosition(targetPos : Vector3)
{
var aimPoint = Vector3(targetPos.x + aimError, targetPos.y + aimError,targetPos.z + aimError);
//Rotaçao da tower
desiredRotation = Quaternion.LookRotation(aimPoint);
}
function CalculateAimError()
{
aimError = Random.Range(-errorAmount, errorAmount);
}
function FireProjectile()
{
nextFireTime = Time.time+reloadTime;
nextMoveTime = Time.time+firePauseTime;
CalculateAimError();
for(theMuzzlePos in muzzlePositions)
{
Instantiate(myProjectile, theMuzzlePos.position, theMuzzlePos.rotation);
Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
}
}
Any idea of what may be causing this?
P.S: Dont worry with the comments that are written in other language
Cumpz, ExyloN
Answer by DisruptionGames · Jul 11, 2012 at 08:26 AM
A personal Preference is to use: Physics.Raycast()
paired with transform.SendMessage()
; It does come off a bit easier since it doesn't require any large amount of Collider calculation and can be manipulated easier.
If you decide not to go with Raycasting the screen shot tends to give off the idea that its not even aiming correctly at the object. I find (Almost always) that Transform.LookAt();
and Quaternion.LookRotation()
are similar but different. If you don't know already you can call Quaternion.LookRotation()
with one simple calculation needed: transform.position - target.position
The Documentation actually shows a great example of this: http://docs.unity3d.com/Documentation/ScriptReference/Quaternion.LookRotation.html So all of this would look at bit like this:
function FireAt(target: Transform){ var relativePosition = target.position - transform.position; var exactRotation = Quaternion.LookRotation(relativePosition); transform.rotation = exactRotation + errorRotation; }
The reason that all of this works is because its calculating the position between the points for a more exact rotation to look at.
Also! For Screen-shots I prefer to use a program called Puush. It sounds weird but its really awesome! You can check it out if you like, it posts instantly to the internet what you decide to screenshot: http://puush.me/
That is the base for Ai$$anonymous$$g so its just what you add as a different function so its a simple copy and paste it. If you want me to help a bit more add me on skype or email me: DevonJavaScript @ Skype DevonJavaScript@Gmail.com
@DisruptionGames - it would be great if you could keep the conversation on UA so that future searchers can see how the complete solution was arrived at.
we're are currently talking in skype but ill write the code when done
Answer by gooongalooo · Jul 11, 2012 at 12:25 AM
...uhmmm...shouldn´t the collider be trigger? what about your tags? everything tagged correctly? is a script attached to your projectile that contains a OnTriggerEnter to check when the projectile hits the box?
the collider is trigger. all tags are correct, there is no OnTriggerEnter on the projectile but i dont think that the problem is in there because it doesnt even hit the box...
here's a screenshot: http://img827.imageshack.us/img827/7784/turretai2.jpg
Your answer
Follow this Question
Related Questions
Placing objects in TD Game 1 Answer
trouble with my lookAt script 0 Answers
Tank AI script that calculates distance of targets and firing angle 1 Answer
Trouble with turret script 1 Answer
Agents in a distributed game 0 Answers