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
1
Question by xVarelax · Dec 07, 2013 at 02:03 AM · 2dmouse position

Fire at Mouse Position, 2d game.

Hi guys!

I'm trying to do a aim system where the bullet will be shot in direction of the mouse position...

I've tried to use Input.mousePosition, but after some researchs , I found that this is relative to pixels not to my transform.position.

I've seen too the Camera.main.ScreenToWorldPoint solution, but that does'nt work how I expected.In my game I instantiate the bullet at the transform.position facing the corresponding side, and after that I apply a vector as the velocity of the bullet.

Does this way of firing right or am I'm going in the wrong direction?

Thank you!

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

2 Replies

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

Answer by robertbu · Dec 07, 2013 at 07:05 AM

Here is a simple spawner script that, on a mouse click, shoots the 'prefab' at the mouse position. I had gravity scale set to 0 on the prefab.

 #pragma strict
 
 var prefab : GameObject;
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
         var pos = Input.mousePosition;
         pos.z = transform.position.z - Camera.main.transform.position.z;
         pos = Camera.main.ScreenToWorldPoint(pos);
         
         var q = Quaternion.FromToRotation(Vector3.up, pos - transform.position);
         var go = Instantiate(prefab, transform.position, q);
         go.rigidbody2D.AddForce(go.transform.up * 500.0);
     }
 }
Comment
Add comment · Show 1 · 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 xVarelax · Dec 07, 2013 at 07:35 PM 0
Share

Ohh thank you for the help dude, this script works very well, but can you explain me how do you came to that conclusion? I'm having some difficulties to understand these Quaternions operations.

I've tried this kind of solution and the error that I'm having is that in this script the velocity is proportional to the vector lenght, so when the mouse is far, the velocity is huge, when the mouse is near velocity is low :

 void Update ()
     {
 
         XvelocityVector =(Camera.main.ScreenToWorldPoint(Input.mousePosition).x) - (transform.position.x);
         YvelocityVector = (Camera.main.ScreenToWorldPoint(Input.mousePosition).y) - (transform.position.y);
         VelocityVectorCte = YvelocityVector/XvelocityVector;
 
         // If the fire button is pressed...
         if(Input.GetButtonDown("Fire1"))
         {
             // ... set the animator Shoot trigger parameter and play the audioclip.
             anim.SetTrigger("Shoot");
             audio.Play();
 
             // If the player is facing right...
             if(playerCtrl.facingRight)
             {
                 // ... instantiate the rocket facing right and set it's velocity to the right. 
                 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3 (0, 0, 0))) as Rigidbody2D;
                 Result = new Vector2 (XvelocityVector, YvelocityVector);
                 bulletInstance.velocity = Result;
             }
             else
             {
                 // Otherwise instantiate the rocket facing left and set it's velocity to the left.
                 Rigidbody2D bulletInstance = Instantiate(rocket, transform.position, Quaternion.Euler(new Vector3(0,0,180f))) as Rigidbody2D;
                 Result = new Vector2 (XvelocityVector, YvelocityVector);
                 bulletInstance.velocity = Result;
             }
         }
     }
avatar image
1

Answer by coreysnyder · Jul 09, 2014 at 09:55 PM

I'm pretty new to this stuff but I think you want to use the "Normalize" method seen here. What it does it keep the direction the same but sets the magnitude to 1. Then you can take that direction * Missile speed and that will pretty much guarantee that your missile files the same speed in the correct direction no matter the pre-normalized distance

Comment
Add comment · Show 1 · 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 xVarelax · Aug 15, 2014 at 04:31 AM 0
Share

Wow dude, this can be really useful, thank you very much for the help!

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

18 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

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Projectile Speeds up when target is farther away 1 Answer

Limiting a Mouse-follow rotation based on parent object rotation 1 Answer

Error with gun aim to the mouse 1 Answer

teeworlds eyes animation 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