- Home /
Enemy Shooting In wrong Direction.
I am creating a 2D Top-Down RPG game. And I have an error with the Enemy Shooting! The Bullet that is being Instantiated is not actually aiming at the player's position, but rather somewhere randomly on the Screen. I was not yet able to figure where the enemy is shooting nor why it is shooting somewhere else. What is supposed to happen is that the Bullet would be shot in the Direction of the Player... but obviously it doesn't.
Thanks for any help I get.
This is the Enemy Shoot Script!
public float bulletSpeed;
public float fireRate;
public float nextFire = 1;
public GameObject bullet;
Transform enemyBullet;
Transform playerPosition;
public Transform shootPoint;
private void Awake()
{
if (!playerPosition)
{
playerPosition = GameObject.FindWithTag("Player").transform;
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (playerPosition)
{
if (Time.time >= nextFire)
{
nextFire = Time.time + fireRate;
CreateBullet();
}
}
}
void CreateBullet()
{
Instantiate(bullet, shootPoint.position, shootPoint.rotation);
}
And this is the actual Bullet Script! (This goes on the Bullet)
public int Speed;
public Rigidbody2D myRigid;
public float lifeTime;
public GameObject playerPosition;
void Start()
{
transform.LookAt(playerPosition.transform);
myRigid = GetComponent<Rigidbody2D>();
playerPosition = GameObject.FindWithTag("Player");
Vector2 sp = playerPosition.transform.position;
Vector2 dir = sp.normalized;
myRigid.AddForce(dir * Speed);
}
// Update is called once per frame
void Update()
{
Destroy(gameObject, lifeTime);
}
Well, I think this is your problem:
Vector2 sp = playerPosition.transform.position;
Vector2 dir = sp.normalized;
myRigid.AddForce(dir * Speed);
The direction needs to be the difference between the projectile and the target, not the target's position. Setting direction as the position of your target is going to give you a nonsensical value, as I'm sure you've noticed.
Try:
Vector2 targ = playerPosition.transform.position;
Vector2 proj = myRigid.transform.position;
Vector2 dir = (targ - proj).normalized;
myRigid.AddForce(dir * Speed);
I did as you have said but now there is a different Error. It seems to be that for 1 The bullets are visible but 2. they aren't moving anywhere and 3. It doesn't seem like all the bullets were ai$$anonymous$$g at the player at all. Also, they where all spawning in 1 area (Next to the Sheep)
the spawn position has to do with that you don't update the shoot position so this stays the same also the bullets have rigibody's?
Answer by Birdman1011395 · Oct 11, 2017 at 08:59 PM
@TreyH hit the nail on the head with what the main issue is: you're using a position vector when you should be using a normalized direction vector. Leaf through the Vector Cookbook section should help you get a handle on things.
That said, there's a lot of issues with your setup otherwise. You shouldn't be using Transform.LookAt(Vector3) in 2D, because it rotates the transform so that its z-axis is pointing at the target. This will cause your bullet to, effectively, disappear from the screen. Also, you only need to flag the bullet for destruction once not, not every frame, so you can remove Update() and put your Destroy() call at the end of start.
All that said, the biggest question is why does BulletScript exist? All it's doing is launching your bullet and flagging it for destruction. I'd move the firing logic for your bullet to the shooter script, because the shooter should be choosing where the bullet goes, not the bullet. You can also easily get the direction vector from the shooter's transform using the right or up variables, if the shooter is tracking the player.
Answer by Trebbit · Oct 11, 2017 at 08:09 PM
maybe it is to simple but try to first assign player position and after that you set your transform.lookat like this the code I attached. Because the first time player position is empty so it will always lookat (0,0,0)
void Start()
{
playerPosition = GameObject.FindWithTag("Player");
myRigid = GetComponent<Rigidbody2D>();
transform.LookAt(playerPosition.transform);
Vector2 sp = playerPosition.transform.position;
Vector2 dir = sp.normalized;
myRigid.AddForce(dir * Speed);
}
I can't really tell if this works because now, the Bullets are rotated in a Way that I can Not see them, and also for some reason the Bullets do not seem to move a single bit too.