Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Kylesauce · Oct 12, 2011 at 08:10 AM · 2djavascripttopdownshooter

Instantiating a prefab/bullet and sending it to my mouse position??

First off, brand new to Unity. It's awesome and so much fun to play around with, but I'm so lost.

Ok, so, I have a top-down, 2-d shooter where I have a character (sphere, for now) that shoots smaller spheres (bullets.)

The game is in the x,z plane, as in the y isn't needed because it's a 2-d game. Now I've been looking for a way to be able to add force to my instantiated bullets, so they travel TOWARD my mouse position. I've been doin' a little searching and doctoring and stuff, but I fear I've dug myself into a hole. Now my script looks like this:

 var prefabBullet:Transform;
 var shootForce:float = 500;
 
 function Update () 
 {
     if(Input.GetButtonDown("Fire1"))
         {
         
         var myPos = transform.position;
         var dist = Camera.main.transform.position.y - myPos.y; 
         var x = Input.mousePosition.x;
         var z = Input.mousePosition.z;
         var dir = Camera.main.ScreenToWorldPoint(Vector3(x, dist, z)) - myPos;            var BulletClone = Instantiate(prefabBullet, myPos, Quaternion.LookRotation(dir));
     
         BulletClone.rigidbody.velocity = dir.normalized * shootForce;
         Physics.IgnoreCollision(BulletClone.collider, collider);
         Destroy(BulletClone,5); //<-- ignore this, i'll fix it later. focus on the top part
         }
 }

Now the problem is, when I click ("Fire1"), the prefabs instantiate and then just chill in space. They don't have any force added to them or launch in any way. Both them and my player gameobject have rigidbodies. Use Gravity is checked off on the bullet prefab. The only thing I could think of is if they get caught in the player gameobject's rigidbody and then just chill in space. If this is the case then how would I fix that? Instantiate it TOWARD mouse position before adding force toward mouse position? I have a feeling i'm on the right track (but maybe not).

Thanks in advance for your help! :D

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

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Writing weapon imprecision 1 Answer

2D Top down shooter, gun is shaking while moving and Cursor solutions 0 Answers

How to make LineRenderer in GameObject Facing? 1 Answer

Creating a wrap around bullet effect like Asteroids. 1 Answer

How to make it so enemies only move towards the player when the player is colliding with an object 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