- Home /
Fire bullet into the direction my player is facing 2D
I am making a 2d megaman like game and I am struggling with making my bullet fire in the direction my player is facing. I am using vector2.right so obviously the bullet will move towards the right, always. But is there a way so that I can make it move to the left when my player is facing the left (same goes for when it's facing right I want both to work)
Hi, I'm Having the same problem, right now my script does the bullet move in both directions but change the direction when my player turn to right or left.
this is the script of the bullet
public float bulletSpeed;
private Transform myTransform;
public GameObject Player;
// Use this for initialization
void Start () {
Player = GameObject.FindGameObjectWithTag ("Player");
myTransform = transform;
}
// Update is called once per frame
void Update () {
float move = Player.transform.localScale.x;
float Speed = bulletSpeed * Time.deltaTime;
if (move > 0){
myTransform.Translate (Vector3.right * Speed);
}
if (move < 0) {
myTransform.Translate (Vector3.left * Speed);
}
if (myTransform.position.x > 10f) {
Destroy (gameObject);
}
}
}
and this is the script for the player
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftControl)) {
Vector3 right = new Vector3 (transform.position.x + (transform.localScale.y / 2), transform.position.y + (transform.localScale.y / 7));
Instantiate (BulletPrefab, right, transform.rotation);
anim.SetTrigger ("Shooting");
if (StopShooting) {
anim.SetBool ("Stop", true);
audio.Play ();
I'm new in this world of program$$anonymous$$g so don't be mad if my question is stupid... forgive my english, i'm still learning.
Thanks.
Answer by supernat · Apr 07, 2015 at 04:37 AM
It depends on how you are modeling the movement. Since you said 2D, are you simply changing which sprite is showing based on the current direction the player is turned toward? If so, you should be able to just use that direction to use vector2.forward, -vector2.forward, vector2.right, and -vector2.right. If you're actually rotating the player's transform, you can use the player's transform.forward vector which will always point in the forward direction of the player in world coordinates.
Your answer
Follow this Question
Related Questions
2d fire from player towards mouse 1 Answer
2D Shooting right only. Doesn't flip direction with character 1 Answer
Instantiate after setting values? 0 Answers
How to destroy only one GameObject??? 0 Answers