Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by bracciata · Aug 03, 2015 at 04:41 PM · c#2dmovementvelocityspeed

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);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How can I make 2D movement less jerky on a controller, with velocity and such? 0 Answers

Player slows down when jumping/velocity changes 1 Answer

[C#, 2D] How do I apply force to a player using vector 3 velocity to move 1 Answer

Game object should move like a fish in 2D C# 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges