Home > Enterprise >  Bullet stays in place but I want it to move
Bullet stays in place but I want it to move

Time:11-05

I wanted to program an enemy who is following my player and shooting at it. The player following part works, but when it shoots the bullets stay in place. Bullet has a Collider which is set to trigger. And the Player has tag - Player assigned to it Here is the script for the bullet:

public float speed;
  private Transform player;
  private Vector2 target;

void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        target = new Vector2(player.position.x, player.position.y);
    }

// Update is called once per frame
void Update()
{
    //Move projectile towards
    //Ja gribam lai lode seko transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);

    transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    if(transform.position.x == target.x && transform.position.y == target.y){
        DestroyProjectile();
    }
}

void OnTriggerEnter2D(Collider2D other){
   if(other.CompareTag("Player")){
       DestroyProjectile();
}
}
void DestroyProjectile(){
    Destroy(gameObject);
}

}

And here is the script for the enemy:

public float speed;
public float stoppingDistance;
public float retreatDistance;
private float timeBtwShots;
public float startTimeBtwShots;
public GameObject projectile;
public Transform player;

// Start is called before the first frame update
void Start()
{
    player = GameObject.FindGameObjectWithTag("Player").transform;
    timeBtwShots = startTimeBtwShots;
}

// Update is called once per frame
void Update()
{
    //Checking distance between enemies
    if(Vector2.Distance(transform.position, player.position) > stoppingDistance)
    {
        //Move enemy towards player
        transform.position = Vector2.MoveTowards(transform.position, player.position, speed * Time.deltaTime);
    //Check if the distance is smaller and make sure if enemy isnt too near to the player
    } else if(Vector2.Distance(transform.position, player.position) < stoppingDistance && Vector2.Distance(transform.position, player.position) > retreatDistance){
        //Too near enemy will stop moving
        transform.position = this.transform.position;
    //if distance is smaller than retreat Back away
    } else if(Vector2.Distance(transform.position, player.position) < retreatDistance){

        transform.position = Vector2.MoveTowards(transform.position, player.position, -speed * Time.deltaTime);
    }

    // Shootig
    if(timeBtwShots <= 0){
        Instantiate(projectile, transform.position, Quaternion.identity);
        timeBtwShots = startTimeBtwShots;
    } else {
        timeBtwShots -= Time.deltaTime;
    }
}

}

CodePudding user response:

The code itself has no problems, so it must be an issue with something else.

One possibility is that GameObject projectile might not be set in the unity inspector. The projectile should also have a bullet component on it, so make sure that is correct.

The cause could also be the bullet speed. By default, it is set to 0 and if it is, it wont move. Double check the value in the inspector to make sure it isn't 0.

A third possibility is that there could be multiple objects with the player tag, causing FindGameObjectWithTag to return the wrong player object. It is unlikely but would be fixed by using a different method of getting the player object such as setting it in the enemy script

CodePudding user response:

Calculate the direction from the bullet to the player by subtracting their positions, then normalizing it:

Vector2 dir = (player.transform.position - transform.position).normalized;

Add direction * speed to the bullets' position:

transform.position  = dir * speed;

Vector2.MoveTowards is essentially lerping, see the docs why: https://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html

If you want to use it, make a step variable like in the example.

  • Related