- Home /
Problem with enemy shooting
Hey guys, I can`t make my enemy shoot with delay and to do it when it`s near my player... Nothing works. No errors, just nothing happens... Please help me...
public Rigidbody projectile; public Transform player; public float playerDistance;
public float speed = 50;
public float delayTime = 0.5f;
private float counter = 0;
void Start () {
}
void Update () {
playerDistance = Vector3.Distance (player.position, transform.position);
if (playerDistance < 1400f && counter > delayTime)
{
counter = 0;
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
}
How do you manipulate counter
variable ? Did you log check there is a running case when distance is less than 1400 ?
Thank you for your response. Still there is a problem with bullet instantiation :/ it`s not moving and it instantiate many bullets in one position .... :(
Thank you iamsidv, but unfortunately it`s not the case . Changed it many times to greater number...and nothing...
NeverHopeless, I checked the distance, everything is fine. i have another scripts that working with this distance and they are fine =\
Answer by iamsidv · Jul 22, 2015 at 06:28 AM
Okay, I see where the problem is now and again, I think you should use Time.time for this functionality.
public float bulletShootInterval = 0.5f;
private float startTime;
void Start()
{
//Get the current time.
startTime = Time.time;
}
void Update()
{
playerDistance = Vector3.Distance (player.position, transform.position);
if (playerDistance < 1400f && Time.time > startTime)
{
//Perform your stuff here.
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
//Increment the time variable by the interval.
startTime = Time.time + bulletShootInterval;
}
}
Hope this helps !!
Answer by NeverHopeless · Jul 22, 2015 at 01:48 PM
Perhaps this:
public float speed = 50;
public float delayTime = 0.5f;
void Start () {
InvokeRepeating("InstantiateProjectileIfNeeded", 0.0f, delayTime);
}
void InstantiateProjectileIfNeeded()
{
playerDistance = Vector3.Distance (player.position, transform.position);
if (playerDistance < 1400f)
{
counter = 0;
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
Since you want to launch a projectile if a distance is less than 1400 but not too frequently there must be a delay of delayTime
. The following script repeatedly invoke a function after delayTime
and if the distance is less than 1400, your logic should execute and no need of counter
variable i guess.
Hope it helps!
@IlanaBrenner, don't use this method in Update
, the delay logic will then be useless. To make it move, first try to move in some other direction than z-axis. e.g., try : instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));
Answer by Veldars · Jul 22, 2015 at 06:11 AM
I think you just forget to increment your counter...
void Update () {
playerDistance = Vector3.Distance (player.position, transform.position);
counter += Time.deltaTime;
if (playerDistance < 1400f && counter > delayTime)
{
counter = 0;
Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
}
}
I hope this help...
Answer by IlanaBrenner · Jul 22, 2015 at 03:22 PM
NeverHopeless , it worked after i added:
void Update(){ InstantiateProjectileIfNeeded (); }
but the bullet still has no speed. It`s makin instantiate and it works..but I need to make it move. Thank you for your help i really appreciate this .
Your answer
Follow this Question
Related Questions
No collision on trigger and character controller 1 Answer
Shoot Bullet Delay Enemy 0 Answers
C # script Enemy shoots left 2d platform. 2 Answers
make Enemy shoot at player 2 Answers
Shoot directions 2 Answers