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 /
avatar image
0
Question by MrFlyingTurtles · Mar 25, 2019 at 02:56 AM · programmingmouse positionprojectiles

Have projectile explode at mouse position?

Hey everyone. I'm making a top down shooter. I have a fireball projectile that starts as a small projectile, but when it hits anything that isn't another projectile, it explodes. The problem with this, though, is that I want the projectile to explode at the Mouse Position when it is first fired, as long as no other colliders block it from reaching that point. I can't seem to get it working right without instantiating another trigger at the mouse position, but if I do this, then different small projectiles could interact with each others destination triggers.

I have tried making the small projectile explode when it's transform.position is the same as the instantiated Destination game object's but it will never work because the position can have really specific floating point values that will never match up across frames.

Any ideas as to how I can make this work? Here's my relevant code:

     public GameObject spellDestination;
 
     Vector3 screenMousePos;
     Vector3 worldMousePos;    
 
 void Start()
     {
         screenMousePos = Input.mousePosition; //this is the position of the mouse on the screen, measured in resolution pixels
         worldMousePos = Camera.main.ScreenToWorldPoint(screenMousePos); //this converts the position of the mouse on the screen to an actual world transform position
         worldMousePos.z = 0;
         Instantiate(spellDestination, worldMousePos, Quaternion.identity); 
 
         Invoke("Explode", weakSpellLifeTime); //destroy the projectile after a certain amount of time
     }
 
 
 void FixedUpdate()
     {
         transform.Translate(0, Time.deltaTime * weakSpellSpeed, 0); //constantly move it forward at a fixed speed
 
         if (transform.position == spellDestination.transform.position)
         {
             isAlreadyExploding = true;
             Explode();
         }
     }
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
Best Answer

Answer by highpockets · Mar 25, 2019 at 09:09 AM

Ok, so it looks like you are getting the mouse position when the projectile is instantiated, but depending on how far away your camera is from where you want the world mouse position to be and ultimately where you want the projectile to explode if no collisions happen, the position might not be precise because you are not passing the z parameter (world units from the camera) to the ScreenToWorldPoint function. You apply the z position to the returned vector after the function has done it's job. You should get the distance the camera is from the plane which the projectile explodes, if you know that the X and Y plane is always located at 0 on the z and you know the camera is always at -10 on the z then pass the function 10 on the z:

 worldMousePos = Camera.main.ScreenToWorldPoint(screenMousePos.x, screenMousePos.y, 10);

If the distance from the camera is always changing, get the distance you should get the distance like so:

 float distance = Mathf.Abs(planeZPos - camera.transform.position.z);


and then pass distance as the final parameter to the function. Now get rid of instantiating the spellDestination trigger and just use lerp for example:

 Vector3 startPos;
 float timeCount = 0.0f;
 
 void Start(){
 
 startPos = transform.position;
 }
 
 void Update(){
 
 transform.position = Vector3.Lerp(startPos, worldMousePos, timeCount);
 timeCount = timeCount + Time.deltaTime;

 if(timeCount >= 1.0f){
 Explode();
 }

 }





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 MrFlyingTurtles · Mar 25, 2019 at 02:07 PM 0
Share

This has the desired effect of exploding at the mouse position, as well as not exploding at the mouse position if there is something in between. However, the problem is that the projectile move speed changes based on how close to the center of the screen you click! How would I make the speed constant?

avatar image highpockets MrFlyingTurtles · Mar 25, 2019 at 02:21 PM 0
Share

Oops, didn't think of that. Try $$anonymous$$oveTowards ins$$anonymous$$d of Lerp:

 float speed = 1.0f; 
 
 void Update(){
 float step =  speed * Time.deltaTime;
 transform.position = Vector3.$$anonymous$$oveTowards(transform.position, world$$anonymous$$ousePos, step);
 
 
         if (Vector3.Distance(transform.position, world$$anonymous$$ousePos) < 0.001f)
         {
            Explode();
         }
 }

That should give you a constant speed

avatar image MrFlyingTurtles highpockets · Mar 25, 2019 at 03:07 PM 0
Share

This works great, thank you!

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

112 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

Related Questions

Multiple Cars not working 1 Answer

[SOLVED] Raycast only hits one side of the box collider 1 Answer

Get the Scene View Position Relative to Camera View Position on Mouse Click 1 Answer

Prior Knowledge 0 Answers

Make a game in C# 0 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