Search Unity

Get normal direction without raycasts

Discussion in 'Scripting' started by suctioncup, Jan 13, 2014.

  1. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    Hello,

    for my game I am generating a triangle, and it acts as a 'sail'. I need to use rigidbody.AddForce to push this sail along, and to do this I need to push it along the angle the triangle is facing, so I need to figure out the normal angle/direction, without raycasting. Is it possible to do this, and how?

    Thanks in advance.
     
    kill390 likes this.
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Won't it just be the rotation of the triangle object?
    Each vertex will have a normal, if you have RecalculateNormals()'d
    Theres a good chance that a simple triangle will have 3 identical normals, which will also be the normal of the face.
     
  3. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    You may just be able to get the facing of the sail.

    transform.forward will give you the Z facing of the object, so move your boat in that direction. This depends on the how the sail is attached and which is the correct facing. You may find that transform.left/right may be the right direction instead.
     
  4. suctioncup

    suctioncup

    Joined:
    May 19, 2012
    Posts:
    273
    I may have the definition of normal wrong...

    $2DFIBts.png

    I have the Vector3 of each 3 points in the triangle, and I want to find the direction of the red line.

    These triangles are generated on the fly, and therefore do not have any rotation, so I cannot figure it out that way. I believe one way to find out the angle is to use a raycast and get the hit.normal, but I would prefer a way without raycasting.

    This is for a single triangle, not for each of 500 triangles in a mesh, btw.
     
  5. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    That would be the vector which is perpendicular to two of the triangle edges.

    First find the two vectors using the vertex positions. Then do the cross product of those two vectors to find the normal.
     
  6. timsk

    timsk

    Joined:
    May 27, 2011
    Posts:
    177
    Why not just use transform.forward?
     
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190