Jump to content

Detecting mouse click on object


photo

Recommended Posts

Hello friends, can you help me with writing a simple script? Because something doesn't work at all  The task is as follows. This is necessary so that when you click (with a valid left mouse button (we are talking about an FPS controller) on a specific object, something happens (something, for example, node.Enabled = false)Like a light switch

Edited by Ostap_Gordon
Link to comment
  • Ostap_Gordon changed the title to Detecting mouse click on object

https://developer.unigine.com/en/docs/2.14.1/api/library/worlds/class.worldintersection?rlang=cs#usage_example

/* ... */
// initialize points of the mouse direction
Vec3 p0,p1;

// get the current player (camera)
Player player = Game.Player;
if (player == null)
	return 0;
// get width and height of the current application window
int width = App.getWidth();
int height = App.getHeight();
// get the current X and Y coordinates of the mouse pointer
int mouse_x = App.getMouseX();
int mouse_y = App.getMouseY();
// get the mouse direction from the player's position (p0) to the mouse cursor pointer (p1)
player.getDirectionFromScreen(out p0, out p1, mouse_x, mouse_y, width, height);

// create an instance of the WorldIntersection class to get the result
WorldIntersection intersection = new WorldIntersection();

// create an instance for intersected object and check the intersection
Unigine.Object obj = World.GetIntersection(p0,p1,1,intersection);

// if the intersection has been occurred, change the parameter of the object's material
if(obj != null)
{
  for (int i = 0; i < obj.NumSurfaces; i++)
  {
    obj.setMaterialParameterFloat4("albedo_color", new vec4(1.0f, 0.0f, 0.0f, 1.0f),i);
    
    // show intersection details in console
    Log.Message("point: ({0} {1} {2}) index: {3} \n", intersection.Point.x, intersection.Point.y, intersection.Point.z, intersection.Index);
  }
}

 

  • Like 1
Link to comment
×
×
  • Create New...