Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Hioozuma · Mar 04, 2020 at 09:16 PM · 2dinstantiatemouseposition

How can i Instantiate my projectile?

Hello folks! So I have this problem where I am blocked with a code, the code in question allows my character to shoot a fireball at the place where I click with my mouse, but, instead of doing that, the fireball goes to the place where the camera is, (the camera is not moving, only the character), I thinks it is a problem with Instantiate and the mousePos, any of you know how I can resolve this issue?

here is the code:

 void Update()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 GameObject sort = Instantiate(projectile, transform.position, 
                     Quaternion.identity);
                 Vector2 mousePos = 
                     Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 Vector2 myPos = transform.position;
                 Vector2 direction = (mousePos - myPos).normalized;
                 sort.GetComponent<Rigidbody2D>().velocity = direction *   
                     vittProjectile;
             }
         }

To maje this code I've watch this tutoriel on how to make a rougue like: https://www.youtube.com/watch?v=Ktcgz1_0JMI&t=1051s

Hope someone will be able to help me ^^

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 tormentoarmagedoom · Mar 05, 2020 at 10:16 AM 0
Share

Hello

if "the fireball goes to the place where the camera is" is because you are misscalculating the vector.

avatar image Hioozuma tormentoarmagedoom · Mar 05, 2020 at 12:37 PM 0
Share

Yeah but it dont really know how to correct that, I'm a total newbie in Unity so I'm not sure at all

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Mar 05, 2020 at 02:11 PM

Thats not Unity, thats math.

I dont know where you instantiate the object or where is the destination. but The direction of the projectile vector must be (Destination position - Origin position) All in world coordinats (not local)

If its not doing what you expect, check where your suposed "origin and destination" really are in the scene.

Comment
Add comment · Show 3 · 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 Hioozuma · Mar 05, 2020 at 06:34 PM 0
Share

I 've tried to coorect my code and came to this one:

 void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             GameObject sort = Instantiate(projectile, transform.position, Quaternion.identity);
             var mousePos = Input.mousePosition;
             mousePos.z = 5;
             var myPos = transform.localPosition;
             var direction = (mousePos - myPos).normalized;
             sort.GetComponent<Rigidbody2D>().velocity = direction * vittProjectile;
         }
     }

So now the fireball are shooting, but not where I want them to, thet always go in the upper right corner of the screen

avatar image tormentoarmagedoom Hioozuma · Mar 06, 2020 at 10:30 AM 0
Share

Hello again.

Come on! aboard your problem step by step. If you are not able to do this, will ahve hard time program$$anonymous$$g. First, i told you that you need to use WORLD coordinates, not Local (if dont know what this means, you must go learn about this,before co$$anonymous$$g ask again!!!)

Lets see your Direction vector, you say its mousepos - mypos which seems correct. So now lets see what this 2 positions are:

$$anonymous$$ousePos i understand is the destination, but Input.mousePosition; is the position of the mouse in the screen, not in the wolrd coordinates, Investigate how to converts mouse position to World coordinates.

myPos is suposed to be the origin, but you are using transform.localPosition wth is the relative position og the object containing ths screipt in reference with its parent, not the world posiotion. Investigate and solve this before come here again!

Good luck!

avatar image Hioozuma tormentoarmagedoom · Mar 06, 2020 at 08:54 PM 0
Share

Hi! Well thanks a lot for your help, I have found a solution and now my code works more properly!!

Here the code I made

 void Update()
     {
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             GameObject sort = Instantiate(projectile, transform.position, Quaternion.identity);
             var mousePos = Input.mousePosition;
             mousePos.z = 10;
             mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 
             var myPos = transform.TransformPoint(Vector3.right);
             var direction = (mousePos - myPos).normalized;
             sort.GetComponent<Rigidbody2D>().velocity = direction * vittProjectile;
         }
     }

Now the isues is that the fire ball is not 100% accurate and dont go excatly where I clic (I will say that tge accurency is arround 97%) and it slow down when i clic near my character But I dont know what cause this despite having done some reaserch. I hope that is the last time i will bother you with my code, but i thinks you have already helped me a lot and i thank you for that ^^

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

347 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shot 2D Projectile in mouse direction always with same speed 1 Answer

Prevent instantiated objects to get mouse hit 0 Answers

Character Shooting Mechanic 2D Issues 0 Answers

Instantiating UI element at mouse position on click 1 Answer

Game Object doesn't instantiate at Mouse Position 3 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