• 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 decoy98 · Jan 16, 2014 at 10:59 AM · instantiateshootingbullet2d physics

Very basic Instantiate Question

Here is my super simple game: https://dl.dropboxusercontent.com/u/33685163/testUnity/testUnity.html

Here is my movement code: http://pastebin.com/HXHTLXyU

My question is how do you make it so that the bullet fires slowly in the direction it's currently facing just like the way tanks shoot in this video: http://www.youtube.com/watch?v=VM6iZgSaeX0

I also want to be able to show the game cursor while playing but when I upload the build the cursor disappears, though I can see it while being in play mode.

The games controls are: mouse click to move the player, and space bar to shoot the bullet.

Thank you very much, I really appreciate all the help from pros like you!

Comment
Add comment · Show 2
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 Dugular7 · Jan 16, 2014 at 12:24 PM 1
Share

Hey there. You may get a few more responses if you post your code in the actual question rather than link to it :) People are sometimes hesistant to click away from the site.

avatar image Sir-Irk · Jan 16, 2014 at 04:15 PM 0
Share

Have you tried using cloneBullet.addForce(Vector2 force)? For example cloneBullet.addForce(transform.forward * 10f);

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by ozku · Jan 16, 2014 at 01:05 PM

moveDirection = transform.TransformDirection(Vector2.right); That should work but I'm not 100% sure because I only know javascript...

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
0

Answer by Sir-Irk · Jan 16, 2014 at 01:05 PM

You want to create a float variable to keep track of how much time has passed since your last shot. In your update do something like this

 float timeElapsed = 0.0f;
 
 void Update(){
     timeElapsed += Time.deltaTime;
 } 

Then in your shooting code check if timeElapsed is >= your firingRate and reset timeElapsed to 0 every time you successfully shoot a bullet. For example setting firingRate to 0.25f would allow a shot every 250 milliseconds.

Edit1: Also, you can use transform.forward to get the current object's forward direction.

I'd personally recommend moving your shooting code to a separate script though. This makes debugging and modification easier.

Edit2: Another tip: you can make a new gameObject with a transform component and make it a child of your player. You can then put your shooting script(should be seperate form the movement script) on this new transfrom object and adjust it's transform.localPostion. This allows you to easily change the position of where your bullets come from. Just make sure that this child object's rotation is set so that it's forward direction is the same as the parent's forward direction.

I'm not sure about the cursor issue. Do you have any scripts that turn the cursor on or off and/or locks the cursor? Check your player settings for your build and see if there is anything that needs to be changed.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Bullet stops when new bullet instantiates pls help 3 Answers

Control amount of bullets 2 Answers

Enemy Damage problem 1 Answer

Invoke is not working properly. 0 Answers

How do I make a 2d bullet object move towards the players original position when it is spawned? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges