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.

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.

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.