- Home /
Help with 2d shooting
so basically I've got a 2d player moving about with a gun. I have a shooting code attached but for some reason, it's always moving right. I think I know where the problem is (line ten where it says transform.right)but don't know how to fix it.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class shootblue : MonoBehaviour {
public GameObject prefab; // instead Missile we use GameObject
public float bulletForce;
void Update()
{
if (Input.GetButtonDown("Fire2")){
GameObject clone = Instantiate(prefab,transform.position, Quaternion.identity);
clone.GetComponent<Rigidbody2D>().AddForce(transform.right * bulletForce,ForceMode2D.Impulse);
}
}
}
What do you mean by "it's always moving right"? Does it move when, you aren't pressing "Fire2". I suggest maybe checking what button "Fire2" is.
Answer by Zoedingl · Aug 02, 2020 at 01:16 PM
Hello, instead of transform.right use transform.forward. Hope this helped.
Your answer
Follow this Question
Related Questions
How can I get my 2D enemy to rotate to look at my player? 2 Answers
Fire bullet into the direction my player is facing 2D 2 Answers
My shooting system wont work, help 1 Answer
2d shooting. 0 Answers
AI reflect shooting 0 Answers