- Home /
How to fire a gun in 2d game
i want to make the bullet go to left side when player turn left and go right when player turn right.
thank you
Javascript
What have you tried to implement already? Show us your code ins$$anonymous$$d of wanting others to do it for you, at least try for hells sake...In JavaScript...
Try this:
var bullet : Rigidbody;
var speed : float = 40;
function Update(){
var newbullet : rigidbody = Instantiate(bullet, transform.rotation transform.position);
newbullet.velocity = transform.forward * speed;}
put this in the empty gameobject of the pistol (the 1 that's instantiating the bullets) tell me how it works
sorry i forget to show my code
your code is error : BCE0018: The name 'rigidbody' does not denote a valid type ('not found'). Did you mean 'UnityEngine.RigidbodyConstraints'?
and this is my code
//this is in player controler code
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))
{
Instantiate (particlePrefab,Vector3(transform.position.x-5,transform.position.y,transform.position.z),Quaternion.identity);
}
//this is in bullet code
var velocity:float = -20;
function Start () {
rigidbody.velocity.x = velocity;
}
function Update () {
}
my bullet is always go to left side
sorry about my english i not good in language skill
it obviously goes always to left side since your velocety is -20, that means it goes with a velocity of negative 20, wich is left side
Answer by MadJohny · Mar 07, 2013 at 09:35 PM
I'm thinking that when you press left key the character will rotate 180 degrees if you were facing right, so, first parent the gun to your gameobject, and create a bullet spawner, when the character rotates, the spawner should be too, and the ray(if you're using raycast) or the object(rigidbody bullets) should be spawned to the right way, if this doesn't work I just came up with something: something like this, btw it may ahve some errors since I'm writing them directly here and not testing them:
var damage : int; //set this to wathever you want to be the damage
var maxDistance : float;
var distance : float;
var bulletSpawner : GameObject;//Create a empty game object and put this in the part where the shot goes of the gun
var turnedLeft : boolean = false;
var turnedRight : boolean = true; // this assumes that your character starts turned right, if he starts turned left change them on the inspector or in the script itself
function Update()
{
if (Input.GetButtonDown("left"))//create button left and right on input settings
turnedLeft = true;
turnedRight = false;
if (Input.GetButtonDown("right"))
turnedRight = true;
turnedLeft = false;
if (turnedRight && Input.GetButtonDown("Fire1"))//assuming that you use mouse 1 to shoot
{
var var hit : RaycastHit;
if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.right), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApllyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
if (turnedLeft && Input.GetButtonDown("Fire1"))//assuming that you use mouse 1 to shoot
{
var var hit : RaycastHit;
if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.left), hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage("ApllyDamage", damage, SendMessageOptions.DontRequireReceiver);
}
}
}
}
Ok I think we're done, well we don't have fire speed, now it's create function ApllyDamage on the enemies script and say something like this:
var health : int = 100;
function ApplyDamage (damage : int)
{
health -= damage;
}
function Update()
{
if (health <=0)
destroy(gameObject)
}
forgive for any errors, I just tried to help, probably have one problem. I putted to shoot left when turned left and right when turned right, that would probably have problems since when you are turned left, you're left is like the camera view if you get what I mean. Anyway try the way I wrote if it doesn't work change every raycast thingies to forward like this: (Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forward), hit))
Answer by BlackWingsCorp · Mar 07, 2013 at 08:20 PM
Now it's getting clearer, your velocity variable is set to -20 so when the player's facing left it goes left but when the player's facing right it should get set to +20 since it's a 2d game & the "c" is the shooting button I assume your movement controls are the arrows so let's try this:
var velocity : float;
function Update(){
rigidbody.velocity.x = velocity;
if(input.getkey(KeyCode.leftarrow)){
velocity = - 20;}
if(input.getkey(KeyCode.rightarrow)){
velocity = 20;
}
}
I guess this should do it.
Your answer
Follow this Question
Related Questions
shot delay in between bullets 1 Answer
Issue with Bullet Movement shooting 0 Answers
Shooting style like enter the dungeon 0 Answers
Hold Button Shooting 3 Answers
fps shooting in the direction of character main cam 1 Answer