Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 BobbleHead · Jan 26, 2013 at 09:55 PM · distancemousepositionray

Shooting at mousePosition

I'm stumped; what I'm trying to do is shoot in the direction of my mouse cursor, after searching through code; I couldn't quite find the answer; this is my attempt:

        if (Input.GetButton ("Fire1")) {
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var distance: float =14;
         var hitPoint : Vector3;
         if (Physics.Raycast (ray, distance)) {
             hitPoint = ray.GetPoint(distance);
             var direction : Vector3 = hitPoint - transform.position;
             Instantiate (enemyBullet, transform.position, transform.rotation);
             enemyBullet.transform.position =(direction * speed * Time.deltaTime);
         }
     }  


The bullet spawns fine; but has ain't moving; that's where I could do with some help as I am kind of lostagain!

Thanks for any suggestions, Bobble

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 Doireth · Jan 26, 2013 at 10:05 PM

Your script only moves the bullet a small distance in that frame. You need to have a script attached to the bullet to make it move every frame. Such as

 transform.position += direction * speed * Time.deltaTime;

Though moving a transform directly like that means it will not calculate collisions correctly. Instead, attach a rigidbody to the bullet and move using

 bulletRigidbody.AddForce(direction * speed);

Check out http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html for information about getting collision information. The line "Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached." is important for your needs.

Comment
Add comment · Show 2 · 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 BobbleHead · Jan 27, 2013 at 12:01 PM 0
Share

So; I've made progress I went by what you suggested and came up with the following:

however the bullets seem to be shooting at an angle and not along the x,y plane as such. Any ideas on how I can tweak it so that the bullets shoot straight and not at an angle?

        if (Input.GetButton ("Fire1")) {
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         var distance: float =12;
         var hitPoint : Vector3;
         if (Physics.Raycast (ray, distance)) {
             hitPoint = ray.GetPoint(distance);
             var direction : Vector3 = hitPoint - transform.position;
             var bullet = Instantiate(enemyBullet, transform.position, transform.rotation);
             bullet.rigidbody.velocity = direction * speed;
         }
     } 

Thanks for any suggestions!

avatar image Doireth · Jan 27, 2013 at 06:06 PM 0
Share

You should never modify velocity directly as it can lead to weird results from the physics engine (see http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-velocity.html).

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

10 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

Related Questions

Raycast hitting below mouse position :( 0 Answers

How to determine multiple points along a ray? 1 Answer

How to make the object stop moving within a certain distance with the target. 1 Answer

How can i tell the distance from a raycast? 1 Answer

SORPG: Distance problem? 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