Search Unity

Question How to use uv coordinates

Discussion in 'Scripting' started by lyki1911, Jan 16, 2023.

  1. lyki1911

    lyki1911

    Joined:
    Sep 1, 2021
    Posts:
    5
    Thanks, no need for help anymore!
     
    Last edited: Jan 17, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    What does this mean? You can get the .textureCoord of anything you hit with raycast... now what do you want to do with it? Remember the UV coordinates are only meaningful for a given texture.
     
  3. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,639
    Last edited: Jan 16, 2023
    Kurt-Dekker likes this.
  4. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    also remember that uv coordinates are not unique on a mesh. an entire mesh can map to a single coordinate theoretically. this is important if you were planning on changing the pixels of a texture, as every part of the mesh that maps to the changed pixel would reflect that change, not just the hit point.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I think I completely misread OP's original post.

    OP if you just need click-to-place or click-to-move in world space coordinates Unity, hit Youtube for some tutorials... it's super-simple, just that call that @kdgalla notes above, one or two lines of code.
     
  6. lyki1911

    lyki1911

    Joined:
    Sep 1, 2021
    Posts:
    5
    Sorry for explaining poorly, so I want to spawn another circular quad object that I have as a mark on the map where the player should go.

    I do this with RaycastHit.texturecoord to get the uv coords and with that try to instantiate a red circle on the map where I clicked. Currently it just goes to the top left of the world not the map even.

    https://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html
     
    Last edited: Jan 17, 2023
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,004
    Right, just as Kurt said, UV coordinates would not help you here. UV coordinates live on the surface of a particular mesh, essentially seperately for each triangle. A seperate quad mesh is a completely speparate object with it's own mesh and completely seperate UV coordinates which may map a different texture onto the surface of that quad. You need to place that quad object in the world at the right position. While it is possible to figure out which worldspace position(s) a certain UV coordinate maps to, it would be total overkill to go that route. When you use a Raycast you already get the
    hit.point
    which is the worldspace position of the hit. That's where the quad should go. Of course when it's a circle texture on that quad, as long as the pivot of the quad is in the center, it should be spot on. The rotation of the quad may need to be adjusted, but that depends on the quad you're creating at the hit point. In most cases using the hit.normal vector and Quaternion.LookRotation to construct a rotation should work.
     
  8. lyki1911

    lyki1911

    Joined:
    Sep 1, 2021
    Posts:
    5
    Hey, got it to work with hit.point thank you!
     
    Kurt-Dekker likes this.