Hello. I’m hoping to get a little help with a problem I’m having. Fair warning, I’m a total noob, so dumbed down answers would be appreciated. The problem I’m having is with an enemy unit I’m creating in a platformer style game. The code below is attached to the prefab projectile which is instantiated in the enemy script. When the enemy unit fires a projectile, it starts off going in the correct direction, but when the player unit moves, the bullet curves mid-air according to his movement. I’m having trouble figuring out how to make the bullet fire towards the player, but fly in a straight line regardless of the player’s movements. Code below. Any help will be appreciated. Thanks in advance.
using UnityEngine;
using System.Collections;
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
public float bulletSpeed = 5;
Vector2 playerPos;
Vector2 startPos;
void Start () {
startPos = transform.position;
}
void Update () {
playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
Vector2 bulletDirection = (playerPos - startPos).normalized;
transform.Translate(bulletDirection * bulletSpeed * Time.deltaTime);
}
}