- Home /
ai help: fire shots
I need help fixing the errors in my script please note if you see anything that will go astray from the description of the script.
this script will be attached to a child of the AI. a cube collider is attached to an axis as a child. it give a firing range to the AI so if i collide with the cube the AI will fire once a second only while you are colliding with the cube. the cube is a trigger so i don't worry about my bullet colliding with any thing other than the player.
Okay I've been having problems with collision lately with more than this. So I may need help with that too. If it helps my player has a character controller. This AI is a rigidbody.
var bulletPrefab:Transform;
var savedTime = 0.0;
var theForce = 1000;
function OnTriggerStay (other : Collider)
{
if(other.gameObject.tag == "Player")
{
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
function Shoot(seconds)
{
if(seconds!=savedTime)
{
var bullet = Instantiate(bulletPrefab, transform.Find("Firepoint").transform.position,
Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * theForce);
savedTime=seconds;
}
Please indent your code by 4 spaces or 1 tab before pasting, or select your code and click the code button. I've fixed it for you this time, but not everyone can do that.
And I don't really know what it isn't doing - apart from something about collision, but what?
well thats the thing isn't it its not doing any thing Ah I'm sorry bout the indenting ill fix it directly on here next time. I believe its not picking up the collisions, sorry again sometimes when I try to give enough info, I for get the important things.
i all ready know the shoot function works if used it on other scripts
If you think it may be a collision problem...test the collision to all extents! Doesnt help to be looking for a moose in the desert..
By this i mean, have you debugged the basic parts of the collision?
function OnTriggerStay(other:Collider)
{
if(other.tag=="Player")
{
Debug.Log("Player Was HIT!");
if(oddeven)Debug.Log("ODD EVEN");
}
}
Answer by TheDavil86 · Jun 24, 2012 at 04:40 AM
Ok here is what I recommend, use a raytrace to determine if the player is visible to the gun before shooting. Like this:
function TargetVisible () : boolean {
// Raycast from me to target position
var hit : RaycastHit;
if (Physics.Linecast (transform.position, target.position, hit))
return hit.transform == target;
// Return false since can't see target
return false;
}
With this you'll need to declare a variable for target which would be the player. Then prior to shooting just say:
if (TargetVisible)
Shoot();
well this may be helpful but it will call for more scripting. I don't want my AI attacking beyond its viewing radius.
okay I've been having trouble putting your piece into my script.
var bulletPrefab:Transform; var savedTime = 0.0; var theForce = 1000; var target:Transform;
function TargetVisible () : boolean { // Raycast from me to target position var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target;
// Return false since can't see target
return false;
if (TargetVisible)
{
var seconds : int = Time.time;
var oddeven = (seconds % 2);
if(oddeven)
{
Shoot(seconds);
}
}
}
function Shoot(seconds) { if(seconds!=savedTime) {
var bullet = Instantiate(bulletPrefab,
transform.Find("firerange").transform.position,
Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * theForce);
savedTime=seconds; } }
Your answer
Follow this Question
Related Questions
Fast object on collider not working 1 Answer
Collisions not being detected at certain distance 1 Answer
Make AI run from Player 1 Answer
Why does my AI miss me every few feet? 1 Answer
How to delay time reset on collision? 2 Answers