Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Get UV coordinate from collision

Discussion in 'Scripting' started by cecarlsen, Aug 15, 2008.

  1. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    Hi Forum

    I want to change the color of a pixels in a texture directly at the coordinate of a collision.

    I can easily get the texture coordinate by making a RayCast (using RaycastHit.textureCoord) against the collider, but I don't get that specific information from a Collision object received in an OnCollisionEnter function. I guess I could create a new RayCast using the ContactPoint information in the Collision object – but there must be a better way.

    I can't find any topics on this. Any Ideas?
    ~ce
     
  2. cecarlsen

    cecarlsen

    Joined:
    Jun 30, 2006
    Posts:
    858
    ...I guess not. So this is how I solved it:

    Code (csharp):
    1. public void OnCollisionEnter( Collision collision )
    2. {
    3.     RaycastHit hit = new RaycastHit();
    4.     Ray ray = new Ray( collision.contacts[ 0 ].point -collision.contacts[ 0 ].normal, collision.contacts[ 0 ].normal );
    5.     if( Physics.Raycast( ray, out hit ) ){
    6.         Print( hit.textureCoord );
    7.     }
    8. }
     
  3. cmz-neu4590

    cmz-neu4590

    Joined:
    Apr 14, 2016
    Posts:
    7
    For more precision you could also get the average of all collision points in the array if you don't want multiple collision points for one object or if it happens to collide in various points and choose one that is kinda off.
     
    vincurekf likes this.