- Home /
This post has been wikified, any user with enough reputation can edit it.
2d shooting.
I want that my shotgun would shot where it points. But now it only shoots left or right. How can i make it shoot where i point. My shotgun follows cursor.
My script:
var Spawn : GameObject;
var speed : float;
var bullet : Rigidbody2D;
var facingR : boolean;
var CanFire : boolean;
var FireRate : float;
function Start () {
facingR = true;
CanFire = true;
}
function Update () {
if(Input.GetKeyDown(KeyCode.A)) {
facingR = false;
}
if(Input.GetKeyDown(KeyCode.D)) {
facingR = true;
}
if(Input.GetButton("Fire1")){
Fire();
}
}
function Fire() {
if(facingR == true && CanFire == true) {
clone = Instantiate(bullet, Spawn.transform.position, Spawn.transform.rotation);
clone.AddForce(Vector2.right * speed);
audio.Play();
CanFire = false;
yield WaitForSeconds (FireRate);
CanFire = true;
}
if(facingR == false && CanFire == true) {
clone = Instantiate(bullet, Spawn.transform.position, Spawn.transform.rotation);
clone.AddForce(Vector2.right * -speed);
audio.Play();
CanFire = false;
yield WaitForSeconds (FireRate);
CanFire = true;
}
}
Comment
Your answer
Follow this Question
Related Questions
slingshot script - velocity help needed 1 Answer
Shooting 2D - how to fix 1 Answer
2d shooting. 1 Answer
Instantied 2D Prefab Is Invisible 1 Answer