- Home /
Using the arrow keys to shoot using c#
Hey I'm using this simple code for my player to shoot. But how do I get the player to shoot in each direction of the arrow keys? so far he only shoots right. using UnityEngine; using System.Collections;
public class arrowKeysShoot : MonoBehaviour {
public Transform Gun;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("up")) {
Instantiate(Gun, transform.position,Quaternion.identity);
}
}
}
How can I get him to shoot up when up is pressed and etc for the other directions? Thank you!
Answer by Regone · Oct 04, 2014 at 01:10 PM
u might want to test this option
public class arrowKeysShoot : MonoBehaviour {
public GameObject Gun;
public GameObject bulletgo;
public int shootforce;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown ("up")) {
GameObject bullet = Instantiate (bulletgo, Gun.transform.position, Gun.transform.rotation) as GameObject;
bullet.rigidbody2D.AddForce (bullet.transform.forward * shootforce);
}
}
}
u might want to change the bullet.transform.forward to bullet.transform.up if u want to change the axis it works on i just dont know on what axis u work on
hope u understood something out of my sloppiness
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Using RayCast to do Continuous Shooting? 1 Answer
How to prevent my Gravity gun from picking up nothing 2 Answers