Reputation: 579
I can not seem to get the raycast to hit the mesh collider. I need this to get the texture uv coordinates.
if (Input.GetMouseButtonUp(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100f))
{
// We've hit this mesh b/c its the only one we have ...
var texture = meshRenderer.material.mainTexture as Texture2D;
var pixelUV = hit.textureCoord;
print("pixs uvs" + pixelUV.ToString());
pixelUV.x *= texture.width;
pixelUV.y *= texture.height;
var position = new Position(pixelUV.x, pixelUV.y);
print("Position: " + position.x + " " + position.y);
texture.FloodFillBorder(position.x, position.y, fillColor, borderColor);
texture.Apply();
}
}
My game object has this script with the update function checking for input, as well as mesh filter, mesh renderer, and mesh collider. The mesh is a simple quad made from unity's menu.
What am I doing wrong? I just do not understand why it is not hitting the mesh. The camera's z position
is -10
and the mesh is at 0
. Placing differently gives the same results.
Upvotes: 0
Views: 4544
Reputation: 241
You can check the Mesh component and see if 'IsTrigger' is checked but may i suggest something else?
Unity does whole this ray casting for you by default. MonoBehaviour class has functions such as OnMouseDown() or onMouseDrag() etc. where a GUIElement or in your case an object with Collider component gets these events. So if your game object has a collider component just implement one of these methods and you are ready to go.
This may you can keep your update function clean.
Check: http://docs.unity3d.com/ScriptReference/MonoBehaviour.html
Upvotes: 0