Several people have been asking for code that takes a coordinate from where the mouse is clicked so that they could send an entity to that position or create something where the mouse is clicked. I was given such a code on this forum about a year or two ago, an indispensible piece that I've used many times afterwards. I don't remember who it was originally who gave it to me. If I did, I'd thank them and prolly send them a christmas present every freaking year until one of us died
Anyways, this code below will allow you to move or create an object at where the mouse position is.
Code:
var from[3];
var to[3];
function mouse_trace()
{
from.X = MOUSE_POS.X; //set from.x to mouse_pos.x
from.Y = MOUSE_POS.Y; //set from.y to mouse_pos.y
from.Z = 0; //set from.z to zero
vec_set(to,from); //copy from to to
vec_for_screen(from,camera); //convert from to 3d coords
to.Z = 3000; //set to.z to 3000
vec_for_screen(to,camera); //convert to to 3d coords
return(trace(from,To)); //return the distance between from and to
}
Like any trace, this modifies the built-in variable "target" which can then be used to send an entity to where the mouse is clicked or place an entity at that spot.
And below is an example of this code in action, placing a beacon when the mouse is right clicked for an RTS game. It also gives the unit a target to walk to:
Code:
var unitTarget[3];
function setTarget()
{
if(!levelLoaded) { return; }
result = mouse_trace(); //trace from mouse to 3d pos.
if(you == null)
{
vec_set(unitTarget,target); //copy mouse_trace target to unitTarget
ent_create(targBMP,unitTarget,beacon); //create the beacon
}
return;
}
on_mouse_right = setTarget();
So I hope this helps a few people as it has helped me. I've used it to fire rockets, shoot wizard blasts, send an entity from one point to another and place beacons on the map. Since I found myself posting this on other threads to respond to similir questions, I thought i'd give it away.