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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by GeneralMidi · Apr 25, 2013 at 06:14 PM · c#inputmouse

Projection to mouse position in isometric game

Hi,

I have an orthographic camera which is sat directly behind my character, raised somewhat and looking down on him. I am aiming to fire a prefab in the direction of the mouse. I've looked all over and I feel like I'm close but I can't quite get the right results.

                 //float dist = Camera.main.transform.position.y-transform.position.y;                
                 //Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
                 Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.mainCamera.transform.position.y);
                 Vector3 targetPos = Camera.main.ScreenToWorldPoint(mousePos);                        
                 targetPos.y = (float)(transform.position.y + 1.3);
             
             
             
                 Vector3 spawnPos = new Vector3((float)(transform.position.x), (float)(transform.position.y + 1.3), transform.position.z);
                 GameObject abil = (GameObject)Instantiate(Resources.Load("fireball"), spawnPos, Quaternion.LookRotation(targetPos));
                 abil.GetComponent<Fireball>().Ignore = "player";
                 abil.transform.LookAt(targetPos);
                 abil.rigidbody.AddForce(abil.transform.forward * ability.Speed);


This code only fires 'north', towards the top of the screen, but sort of loosely goes to the mouse position, but only within a restrictied cone from the player.

If I uncomment the first two lines in place of the third, I get different behaviour. The fireballs go southwards, loosely towards the mouse position, unless I go to the very top of the screen where it will begin to aim northwards.

Clearly I've got something wrong here. I did try something to do with creating a plane and raycasting, but that had similar effects the code shown.

Hope this is clear. Appreciate any replies.

Comment
Add comment · Show 1
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 GeneralMidi · Apr 25, 2013 at 06:58 PM 0
Share

Quick update to this: If I comment out this line:

 targetPos.y = (float)(transform.position.y + 1.3);

And change this line:

 Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 5);

Then the prefab moves towards the cursor, but it flies upwards. I was setting the Y so it fires directly forward from the spawn point. It seems like my code isn't taking into account camera rotation or something.

With the code in the original post, the X and Y seem right, but the Z is wrong.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GeneralMidi · Apr 25, 2013 at 08:09 PM

I got it working. Incase anyone googles this and would like to try the code:

                    Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                    RaycastHit Hit;
                 Vector3 targetPos;
          
                 if (Physics.Raycast (ray, out Hit, 100)) 
                 {
                 
                     if (Hit.collider.gameObject.tag == Tags.terrain) 
                     {
                         Debug.DrawRay (transform.position, transform.position, Color.red);
                     }
                 
                 }
                 targetPos = Hit.point;
                 targetPos.y = (float)(transform.position.y + 1.3);
                 targetPos.z -= 1;
             
                 Vector3 spawnPos = new Vector3((float)(transform.position.x), (float)(transform.position.y + 1.3), transform.position.z);
                 GameObject abil = (GameObject)Instantiate(Resources.Load("fireball"), spawnPos, Quaternion.LookRotation(targetPos));
                 abil.GetComponent<Fireball>().Ignore = "player";
                 abil.transform.LookAt(targetPos);
                 abil.rigidbody.AddForce(abil.transform.forward * ability.Speed);
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 Hexyes · Aug 09, 2017 at 09:46 PM 0
Share

what manner of component is fireball, is that the collider?

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Checking if the Mouse Has a Middle Button 1 Answer

C# inputString mouse Keys? 1 Answer

Standalone gets mouse input when in background UNET 1 Answer

Keep objects current rotation, with Input Mouse X 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