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 primus88 · May 28, 2013 at 07:37 PM · guimouseclickspell

How to cast spells by mouse click

Hello,

Sorry for the title as it is not very precise. My problem is this:

  1. I select the spell I want (by clicking it's cast button or designated hot key)

  2. How can I then cast it somewhere on the map by using the mouse ?

Is there something like 'wait for mouse click' ? I really have no idea even how I can logically implement this.

Any help would be much appreciated.

Thank you.

Comment
Add comment · Show 4
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 ExpiredIndexCard · May 28, 2013 at 07:54 PM 0
Share

I still don't understand what you need. Do you need to instantiate a particle system or do you want a raycast for precision? Please $$anonymous$$ake it more precise

avatar image primus88 · May 28, 2013 at 08:59 PM 0
Share

Not referring to particles.

You know in RTS games, where you have a spell, you select the spell you want to use, the mouse pointer, for example, turns into casting mode and then you cast it on the ground with your mouse.

I don't know how to make that transit from spell select to casting it on the ground.

Right now I have the GUI, to select the spell and apply damage to enemies, but it is a placeholder as I can't target with my mous where to cast it.

I hope this explains a bit more.

avatar image gardian06 · May 28, 2013 at 09:32 PM 0
Share

do you want to target only a specific enemy (when I click enemyX direct effect), or just any point in the game would (when I click the ground near enemyX area effect), or both?

avatar image primus88 · May 29, 2013 at 08:05 PM 0
Share

I need it to cast it anywhere in the game. If AOE detects enemy, substracts health.

2 Replies

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

Answer by gardian06 · May 28, 2013 at 09:26 PM

you are probably looking for the onMouse family of messages: OnMouse for these you do need to realize that the system will send out a ray from the mouse pointer to the first physics layer, or GUI object it hits.

more specifically on the implementation. I will make a few assumptions: you can only target units with these spells, and that you have some kind of static library that can be called to "cast" from (this can be a static class that holds any possible spell/ability that could be cast in the game)

in your onMouse message receiver (held in the units class) have it call back to the library so that it can give either its location, or some key identifier/attribute to determine if the spell/ability could be used on them, or so that the static library can fire the spell.

after further information there are 2 possible implementations that I can think of.

  • using the static library concept from the previous part of the answer have the library maintain an

       Update(){
             if(pendingSpell != null && Input.GetKeyUp(KeyCode.Mouse0)){
                 RaycastHit hit = new RaycastHit();
                 Ray ray = camera.ScreenPointToRay(Input.mousePosition);
                 if(Physics.Raycast(ray, out hit){
                     pendingSpell.Cast(hit.point);
                     pendingSpell = null;
             }
         }
    

just be sure to have the "casting unit" tell the static library what it is casting. (note: I am treating these spells as script objects which can still be held by a static library either statically, or as a component of the static library, or the unit that "possesses" this spell the difference would be the level of abstraction, duplication, and/or uniqueness that you wish to have among these spells)

  • to do as above, but have the update code be on each of the units (this can get rather intricate, and it would make things like networking a nightmare to debug. though I would replace the on each unit with having a library for each "player" (counting AI player) granted you can't talk to it statically you could have all units spawned just know of the library for the player that created it.

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 gardian06 · May 29, 2013 at 09:23 PM 0
Share

$$anonymous$$or note: using Get$$anonymous$$eyUp this is the frame when the button is released. given where I preface this with a check against an object you could ins$$anonymous$$d use Get$$anonymous$$ey, or Get$$anonymous$$eyDown, but if your implementation disallows such a null check you would probably want to keep the Get$$anonymous$$eyUp

avatar image primus88 · May 30, 2013 at 10:36 PM 0
Share

Thanks it worked well until now. But it's not perfect, because it spawns the spell in a strange position. This is the code :

 var ray = CameraUI.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
     if(Physics.Raycast(ray,hit)){
     //Debug.Log("Now I see a cube");
     if(hit.collider.tag == "RayEnemy" ){
     Debug.Log("I see a unit");
      if(Input.Get$$anonymous$$ouseButtonDown(0))Instantiate (particle, hit.point, transform.rotation);

The problem is that the particle instance is spawned somewhere related to the detected collider but if I move the camera I can see that in fact the particle instance hit in mid air somewhere between collider and camera or around collider.

How can I spawn the particles (a lightning strike spell) right under collider position (on the ground under my unit because the collider is centered on the unit).

THanks

avatar image gardian06 · Jul 09, 2013 at 08:49 PM 0
Share

if I have not fully answered your question feel free to unmark it as the answer. (an answer make usually tells the user that they do not need to do any more help with it.

you probably don't want to be calculating the raycast every frame. the reason I put the raycast after the if(Input....) was that successive Raycasts can get costly.

  • make sure that the camera being referenced is Camera.mainCamera

  • that the ray hit the correct thing Debug.Log(hit.collider.name);

  • if you have anything GUI wise being drawn over top of this object it will intercept the Raycast unless you are using layer masks (I will not get into these as I dislike bit shifting)

avatar image
0

Answer by darthbator · May 28, 2013 at 09:06 PM

Shoot a ray from the Camera at the ground and then "cast" or "shoot" the spell at or towards the location of the vector hit. You'll want to use ScreepointToRay to get the mouse position and populate a ray object. Then you'll use a raycast to 'shoot" the ray. You should then be able to get the ray strike location and cast the spell there. You're basically translating the Vector2 screen coordinate location to a Vector3 world space location to cast the spell.

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 gardian06 · May 28, 2013 at 09:28 PM 0
Share

probably want to tie this into a button press like input.get$$anonymous$$ey____("fire")

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

15 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

Related Questions

how to call a gameobject in one function(mouse selection) to another function (GUI) 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Any problems with this script? 1 Answer

Card matching game 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