- Home /
Problem creating a 2D scroller shooting game
Hello everyone , I have a problem with my 2D scroller shooting game (like metal slug), basically I'm not able to create a script to shoot in front of the player . I've created one , but the bullet doesn't move in front of the player , and it keep staying where it was spawned (the actual player position). Here's my script :
var Sparo_pref : Transform;
function Update ()
{
if(Input.GetAxis("Spara")){
var bullet = Instantiate(Sparo_pref, Vector3(transform.position.x,transform.position.y,0), Quaternion.identity);
bullet.rigidbody.AddForce(bullet.transform.forward * 5);
}
Can someone help me, please? I have been searching a solution for couple of days , but nothing happens...
Edit : code sample ;)
Try adding constantForce to the bullet. I'd also define the prefab as a Rigidbody. I'd also spawn 1 unit in front of player, ins$$anonymous$$d of 0 on z axis (your choice). You also need to have the bullet ignore collision with the object it is spawning from, if that object has a collider.
var Sparo_pref : Transform;
var force : float = 100f;
function Update ()
{
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
var bullet = Instantiate(Sparo_pref, transform.position + transform.forward, Quaternion.identity);
if(!bullet.gameObject.GetComponent(ConstantForce))
{
bullet.gameObject.AddComponent(ConstantForce);
bullet.constantForce.force = bullet.transform.forward * force;
}
else
bullet.constantForce.force = bullet.transform.forward * force;
if(bullet != null)
Physics.IgnoreCollision(this.collider, bullet.collider, true);
}
}
Please either accept an appropriate answer, let us know it has or has not been resolved, or close your question!
Answer by AlucardJay · Jul 21, 2013 at 02:55 PM
The problem is you want to add an instantaneous force, where AddForce is applied over time in physics. For an instantaneous force in your case of shooting a bullet and applying force only once, you need to use ForceMode.Impulse :
rigidbody.AddForce( transform.up * jumpForce, ForceMode.Impulse );
or using your script :
bullet.rigidbody.AddForce( bullet.transform.forward * 5, ForceMode.Impulse );
Links :
Have you tried increasing the force? 5 is a very small force! Also check you arn't instantiating where it is hitting the player, and the player is absorbing all the force
Answer by Dimling · Jul 21, 2013 at 11:30 AM
First of all it would be nice if you formated your code by using the "Code Sample" button so its easier for us to read.
First of all what I do when I create a 2d shooter game is to create a sphere in front of the player, right before the gun, that represent the spawn point for the bullet. Lets just call it "FireSpawnPoint". This one must of course not hit the collider of the player. After that I make a prefab that represent the bullet. For intsance just a spehere for this as well. Lets just call this one "BulletPrefab". After this its time to write some code.
public void Fire(){
GameObject clone = Instantiate(BulletPrefab, GameObject.Find("FireSpawnPoint").transform.position, Quaternion.identity) as GameObject;
clone.rigidbody.AddForce(Vector3.right * 5000);
}
This script spawns a bullet in front of the player, at the spawn point, and then fires its away.
Have I just to copy you code , replacing $$anonymous$$e? If yes , it doesn't work :(
Well, you have rewrite the code so it matches your object names!
Your answer
Follow this Question
Related Questions
Shoot Bullet At Touch Position : 2D 1 Answer
Need a script for a gun that shoots bullet using raycast 2 Answers
Bullet not moving 1 Answer
2d shooting Left / Right 1 Answer
Troubles With A Shoot Script 1 Answer