Jump to content

Mouse over a part


Recommended Posts

Ok, I'm stuck on something... I'm using a raycast to get the part under the cursor in flight, and everything is fine.

Unfortunatly, I recently discover that the raycast do not hit some vessel parts (like the small scientific parts).

I think it's because KSP remove colliders of some parts for an unknow reason. What can I do for that ?

Do I need to change the method ? Is there another option to detect part mouse hover/exit ? It really bother me to add a component on every part for adding onmousehover method, as raycasting is far better for what i'm working on.

Edited by KospY
Link to comment
Share on other sites

Did you try to change the layerMask used for the raycast ? The part should have a collider but may use a different layer. You could check the part.gameObject.layer of the part and then adjust the layer mask.

Link to comment
Share on other sites

Unfortunately this seems to be an issue where a small part hides inside the collider of a larger object. When I struggled with this (back in KSP 0.24, no guarantees things have not changed) you could zoom in so the small part filled 90% of your screen and you would still select the larger object when you clicked on it.

I spent quite a long time on this before I gave up and simply listened for the right-click part menu on the small parts of this type.

I'd love to know how Squad does it, it is obviously possible because they open the right-click menu correctly for the small part, but I spent hours on this myself and was not able to figure out how.

It is not a layer issue, the raycast I was using had no mask applied and so should have hit every layer.

The one bit of knowledge I have since gained that I did not know at the time I looked at this, is the part physicless/massless? I wonder if that is the key to what is selectable or not.

D.

Link to comment
Share on other sites

I finally found a way to do it :)

I checked and the collider exist on this parts too, so it's not a problem with a missing collider. However, I found that the attached rigidbody is changed to the parent part.

I used "hit.transform.gameObject.GetComponent<Part>();" to get the part component, but I think hit.transform.gameObject return the rigidbody gameobject.

To get the correct part component, you need to go throught all parents of the collider gameobject until you find the part. After making my own custom method, I found that KSP already have something to go throught all parents : "UIPartActionController.GetComponentUpwards". I just used it to find the part from the collider and it seem to work.

So, here is the method for people interested :


private Part GetPartUnderCursor()
{
RaycastHit hit;
Part part = null;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 1000, 557059))
{
part = (Part)UIPartActionController.GetComponentUpwards("Part", hit.collider.gameObject);
}
return part;
}

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...