• 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 timo0060 · May 14, 2012 at 02:12 PM · javascriptmouseshootingaimingsidescroller

Bullets follow mouse after shooting in Sidescroller

So as the title states, my bullets continue to follow the mouse around after I soot instead of going in a straight line. So if I move the mouse left, the bullets curve left. This would be great for a homing missile, but I'm not there right now. How do I store the position of the mouse so the bullet only goes to the stored position and not the new one? Here's my code so far to both calculate the mouse position and fire the bullet towards it.

Finding mouse Position Followed by firing towards mouse:

 // this creates a horizontal plane passing through this object's center
 var plane = Plane(transform.position, Vector3.up);
 // create a ray from the mousePosition
 var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 // plane.Raycast returns the distance from the ray start to the hit point
 var distance: float;
 if (plane.Raycast(ray, distance)){
     // some point of the plane was hit - get its coordinates
     hitPoint = ray.GetPoint(distance);
     hitPoint.z = -19;
     }
     
  

direction = hitPoint - transform.position;

 // Firing bullet towards mouse

transform.Translate(direction bulletSpeed Time.deltaTime)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Piflik · May 14, 2012 at 02:17 PM

You have this script on the bullets?

I would add the script your aircraft/spaceship/whatever. Once you have calculated the direction, instantiate a new bullet that faces this direction. The bullet itself just needs a script to move forward.

Comment
Add comment · Show 5 · 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 timo0060 · May 14, 2012 at 02:25 PM 0
Share

I actually calculate the distance on the PlayerScript, and call it in the BulletScript. Here's how I instantiate the bullet in the player script: var instantiatedbullet = Instantiate(bullet,GameObject.Find("SpawnPoint").transform.position, transform.rotation);

The spawn point rotates to face the mouse at all times as well.

avatar image Piflik · May 14, 2012 at 02:30 PM 0
Share

Edit: Use the direction in Start() or Awake() only. $$anonymous$$ove the bullet only forward in Update().

Or instantiate the bullet with 'direction.normalized' ins$$anonymous$$d of 'transform.rotation' (and again just move it forward).

avatar image timo0060 · May 14, 2012 at 03:22 PM 0
Share

When I add the direction.normalized ins$$anonymous$$d of the transform.rotation when I instantiate the bullet, I just get the following error:

BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.Vector3, UnityEngine.Vector3)' was found.

avatar image Piflik · May 14, 2012 at 03:25 PM 0
Share

Oh, right...instantiate needs a quaternion, not eulerAngles...

Quaternion.Euler(direction.normalized) should work...

avatar image timo0060 · May 14, 2012 at 06:03 PM 0
Share

That fixed the error, but not the problem. I've posted a video on youtube that will hopefully help solve this problem. You can find it here and if you need any code, just ask.

Here's the link: http://youtu.be/tT09CA-pY9$$anonymous$$

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Aiming with the mouse in a 2d shooter, and then shooting to the mouse 2 Answers

Sidescroller Bullets Follow Mouse After Being Shot 2 Answers

Trouble with aiming with the mouse in a 2D sidescroller 0 Answers

My Bullets aren't moving... 1 Answer

Code Converstion into javascript Help? 2 Answers


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