|
|
Re: entity creat at mouse click
[Re: harry3174]
#177256
01/10/08 05:35
01/10/08 05:35
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
First of all, event triggers such as EVENT_CLICK are used for clicking on entities and stuff, not for mouseclicking in general. So upon calling the function creat(), the engine will never read the part within the if statement. Second, you want to create an entity when you click the mouse, but your while statement will never be executed when you don't click the mouse first, and thus never wait for your input. Third, when you create a view model, you should use ent_createlayer (I assume you want a view entity instead of a world entity as you define an entity as ENTITY*). Plus you place your model directly on the camera.z axis, you wont see it as your camera is now in the model. So, time for the solution: Code:
function main() { level_load("ent_.wmb"); wait(2);
creat();
mouse_mode = 2; while(1) { mouse_pos.x = mouse_cursor.x; mouse_pos.y = mouse_cursor.y; wait (1); } }
function creat() { while (1) //we will always check for user input, every frame { if (mouse_left) //left mouse has clicked! { you = ent_create("earth.mdl", 0, 10); //create earth.mdl view entity, no flags, on layer 10 you.x = mouse_pos.x; you.y = mouse_pos.y; you.z = 40; //40 quants in front of the camera } wait(1); } }
If you wanted a world entity instead, you need some more code for letting it follow the mouse. I'll help you on it if this is the case. Ciao
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: entity creat at mouse click
[Re: harry3174]
#177258
01/11/08 13:24
01/11/08 13:24
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
Well I made a little mistake. I wrote ent_create in my code instead of ent_createlayer. Ent_create makes an entity in the world, where the player walks too. Ent_createlayer makes an entity on the camera view. Thus independent of where the player walks and rotates, it's position will always be in the screen. Seen that you did not know what I was talking about, I think you want the first option  . My apologies, for I have not tested the code. I just blindly trust on my superb programming skills  . So here an improved code: Code:
function main() { level_load("ent_.wmb"); wait(2);
creat();
mouse_mode = 2; while(1) { mouse_pos.x = mouse_cursor.x; mouse_pos.y = mouse_cursor.y; wait (1); } }
function mouseTo3D() { while (1) { if (mouse_left) { vec_set (mouse_click, mouse_dir3d); //get direction vector from mouse to world vec_normalize(mouse_click, 4000); //give it a large distance for tracing vec_add (mouse_click, camera.x); //calculate from camera position
c_trace (camera.x, mouse_click, IGNORE_MODELS); //trace from camera to clicked position (= mouse position + 4000 in depth)
return(target.x); //we return the new 3d vector for further use } wait(1) } }
function follow_mouse() { while (me) { vec_set(you.x, mouseTo3D()); wait(1); } }
function creat() { while (1) //we will always check for user input, every frame { if (mouse_left) //left mouse has clicked! { you = ent_create("earth.mdl", nullvector, follow_mouse); //create earth.mdl and let it follow the mouse } wait(1); } }
Last edited by Joozey; 01/11/08 13:26.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
Re: entity creat at mouse click
[Re: harry3174]
#177260
01/12/08 17:00
01/12/08 17:00
|
Joined: Oct 2004
Posts: 4,134 Netherlands
Joozey
Expert
|
Expert
Joined: Oct 2004
Posts: 4,134
Netherlands
|
My mistake (again), replace the mouseTo3D() with the following code: Code:
var mouse_click[3];
function mouseTo3D() { vec_set (mouse_click, mouse_dir3d); //get direction vector from mouse to world vec_normalize(mouse_click, 4000); //give it a large distance for tracing vec_add (mouse_click, camera.x); //calculate from camera position
c_trace (camera.x, mouse_click, IGNORE_MODELS); //trace from camera to clicked position (= mouse position + 4000 in depth)
return(target); //we return the new 3d vector for further use }
And if you would click the left mouse now, alot of entities are created. To prevent that, replace and type: Code:
if (mouse_left) //left mouse has clicked! { you = ent_create("earth.mdl", nullvector, follow_mouse); //create earth.mdl and let it follow the mouse while (mouse_left) {wait(1);} }
Last edited by Joozey; 01/12/08 17:05.
Click and join the 3dgs irc community! Room: #3dgs
|
|
|
|