Reputation: 1
My game has a level where enemies fly in and fire missiles at the play, they're supposed to target the players local at the moment they were fired but when they're fired they only hit the player when its moving left to right, when it moves forward the missile just fly right overhead.
void Start() //targeting the player
{
playerManager = GameObject.FindGameObjectWithTag("PlayerManager").transform; //NB: it is tracking wherever the playerManager is placed on the X axis, but NOT the playerManager directly
//target = new Vector3(playerManager.position.x, playerManager.position.y); //this is the original line
// target = new Vector3 (playerManager.transform.position); //this was recommended on unity answers but it returns an error.
rb.velocity = transform.forward * speed; //addition, tells it to shoot forward
}
I want the missiles to keep targeing the player as they move along the z axis.
when the player can only move side to side it works as intended but when they move forward the missiles end up flying over head.
this line: target = new Vector3 (playerManager.transform.position); returns the following error message:
Error CS1729 'Vector3' does not contain a constructor that takes 1 arguments
Upvotes: -1
Views: 295
Reputation: 86
You want the missiles to consider the player's z position but the "target" Vector3 in your first option is missing the third argument and so it defaults to (x,y,0) rather than (x,y,z). You could do:
target = new Vector3(playerManager.position.x, playerManager.position.y, playerManager.position.z);
or:
target = playerManager.transform.position;
Note that Vector3 is constructed with three floats, not with a Vector3, which is what playerManager.transform.position is. You can skip the constructor and just set the values equal.
Upvotes: 1