- Home /
Why does my AI miss me every few feet?
If I am standing right in front of him he will hit me then if I back up a little he will start to miss me. After a few more feet back he will start hitting me again. I think I know the problem but not sure how to fix it. this is the code I have and all the info I think you will need to help me.
On my Bullit prefab I have the following set of var's
Mass: 0.1
Drag: 0
Angular Drag: 0.05
Use Gravity: True
Is Kinematic: False
Interpol: None
Collision Detection : Discrete
Constraints: None
This is the code on my AI
var LookAtTarget : Transform;
var damp = 6.0;
var bullitPrefab : Transform;
var savedTime = 0;
function Update ()
{
if(LookAtTarget)
{ // Finds the dif between the two Player position Turret Pos
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
// rotation of the turret turret rotation, var, time that passes between frames
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime *
damp);
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
function Shoot(seconds)
{
if(seconds != savedTime)
{
var bullit = Instantiate(bulltPrefab, transform.Find("SpawnPoint").transform.position,
Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward *1000);
savedTime = seconds;
}
}
And This is my code on my buillit prefab
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "Player")
{
HC.LIVES -=1;
Destroy(gameObject);
}
if(hit.gameObject.tag == "turret")
{
Destroy(gameObject);
}
}
Please try to tidy your code a bit
If you think you know what the problem might be, can you share that information? It'll be faster than trying to guess ; )
Get a lot more data on where it misses, hits and how. Does NESW to the target matter? Does it always miss by shooting too far down? Is it just the one range band, or are there more? The bullet shoots at the firer's facing -- in the Inspector, is Slerp giving odd rotations?
Sorry for the crappy code I am new to scripting/code. When I pause the game and go frame by frame I see the space it skips each time. Sometimes I am colliding with it and others I am not. It moves by 2.96,0.26,2.6 so if I am not in a space that is in increments of 2.9,.26,2.6 from the AI then it will not detect a hit on me. I need it to move at the same speed but update more often I guess??
I do not fully understand Slerp yet.
ChrisD is asking you to at least format the code properly in the question. If it's hard to read, most people will not bother, so you won't get an answer from them.
Answer by Waz · Jul 04, 2011 at 10:53 PM
The problem is you are trying to use a high-speed bullet. With discreet physics, the bullet jumps forwards each frame - sometimes right over you. Either change the Collision Detection mode to "Continuous Dynamic" (which is expensive), or use raycasting to "fire" bullets rather than trying to model them physically.
Ya ray casting another mystery unto its self. That is the next thing I dig into. Thx for all your help and sorry for the shitty code :)
Your answer
Follow this Question
Related Questions
OnCollisionEnter not working with Platform and Ball game 1 Answer
How to detect correct collision from multiple game objects? Separate collisions 0 Answers
OnCollisionEnter being called without the colliders actually colliding 1 Answer
Help with collisions and destroy please 1 Answer
Smooth movement for the block upon player collision,Smooth box movement 0 Answers