- Home /
How to cast spells by mouse click
Hello,
Sorry for the title as it is not very precise. My problem is this:
I select the spell I want (by clicking it's cast button or designated hot key)
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.
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
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.
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?
I need it to cast it anywhere in the game. If AOE detects enemy, substracts health.
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.
$$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
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
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)
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.
probably want to tie this into a button press like input.get$$anonymous$$ey____("fire")