- Home /
 
Bullet not moving from script
Here's my code, what else do I need to make it move for a 2d game.
 Rigidbody2D bulletInstance = Instantiate(bullets, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as Rigidbody2D;
             bulletInstance.velocity = new Vector2(bulletspeed, 0);
 
              Answer by edo_m18 · Aug 04, 2015 at 05:54 AM
What type is the bullets?
I guess that's a GameObject, right?
Try code below:
 GameObject bulletInstance = Instantiate(bullets, transform.position, Quaternion.Euler(new Vector3(0,0,0))) as GameObject;
 Rigidbody2D rigidbody = bulletInstance.GetComponent<Rigidbody2D>();
 rigidbody.velocity = new Vector2(bulletspeed, 0);
 
              Answer by Cepheid · Aug 04, 2015 at 04:13 AM
Well, the usual way I make 2d bullets move is by applying the force on the prefab itself. So, for example I would remove:
bulletInstance.velocity = new Vector2(bulletSpeed, 0);
Then on your prefab bullets I would attach a new Rigidbody2D and create a new script which does the following:
     public float _bulletSpeed;
     public Rigidbody2D _rb;
 
     void Start () {
 
       _rb = GetComponent<Rigidbody2D>();
 
     }
 
     void FixedUpdate () {
 
     _rb.velocity = new Vector2 (bulletSpeed, 0);
 
     _rb.AddForce(_rb.velocity, (ForceMode2D.Force));
 
     }
 
               I hope this was what you were looking for. :)
Answer by Animatick · Aug 04, 2015 at 05:54 AM
mabye this might help you out im building a 2d game that shoots projectiles and this is the scropt that i use
 using UnityEngine;
 using System.Collections;
 
 public class NinjastarController : MonoBehaviour {
 
     public float speed;
     public PlayerMovement player;
     public float RotationSpeed;
     
 
     void start()
     {
         player = FindObjectOfType<PlayerMovement>();
         if (player.transform.localScale.x < 0) 
         {
             speed = -speed;
             RotationSpeed = -RotationSpeed;
         }else if (player.transform.localScale.x > 0){
             speed = speed;
             RotationSpeed = rotation;
         }
     }
 
     void Update () {
         rigidbody2D.angularVelocity = RotationSpeed;
 
         rigidbody2D.velocity = new Vector2 (speed, rigidbody2D.velocity.y);
     
     }
     
 
 }
 
               and this is in my player movement script
 public void FireStar()
     {
         Instantiate (ninjaStar, firePoint.position, firePoint.rotation);
     }
 
               im setup for touch controls so that's why its in its own method but you can put the instantiate code on a regular button press....again i hope this helps if you are having trouble understanding i can explain how thi is all working for you
Your answer