Unity Community


Results 1 to 5 of 5

  1. Posts
    175

    Raycast Normal. Rotation Alignment.

    I am trying to get an axis of a cube to align itself to the normals of an object. Sounds simple enough right?

    How do I get the returned normal that is hit by a raycast to be applied to the rotation of the cube so that alignment occurs?


    I found these things which didn't help.

    http://answers.unity3d.com/questions...casthit-normal

    The unity scripting manual... Which gives a vague description.

    RaycastHit.normal
    Description
    The normal of the surface the ray hit.


  2. Posts
    175
    I found this with some more searching.. I'm not sure if this would work. (I will have to try when I get home)

    I am confused as to how an angle of rotation can be calculated by looking at the normals of an object. Because I am thinking a point of reference must be used to distinguish from one degree to another degree of rotation. For calculating the angle of a normal, does unity use the world space as a reference? If not, how does unity determine the angle of rotation for surface normals?


    http://forum.unity3d.com/threads/839...299#post5299):


    // cast a ray down from the player object
    var hit : RaycastHit;

    function Update () {
    if (Physics.Raycast (transform.localPosition, transform.TransformDirection (Vector3.down), hit)) {
    //if (Physics.Raycast (transform.localPosition, Vector3.down, hit)) {
    // put the player object where the ray hit the terrain object
    transform.localPosition = hit.point;
    // orient the player object with the terrain surface normal
    // only change the X and Z axes, since the Y-axis is rotated by the player
    transform.rotation = Quaternion.LookRotation (Transform.TransformDirection (Vector3.forward), hit.normal;

    }
    }


  3. Posts
    2,857
    Short answer: Use Quaternion.SetFromToRotation() or Quaternion.FromToRotation() to compute a rotation that will rotate the cube's current up vector onto the surface normal, and then apply it to the Transform.rotation field to align the cube.


  4. Posts
    175
    Quote Originally Posted by Jesse Anders View Post
    Short answer: Use Quaternion.SetFromToRotation() or Quaternion.FromToRotation() to compute a rotation that will rotate the cube's current up vector onto the surface normal, and then apply it to the Transform.rotation field to align the cube.
    Is there a simple script example that I could see that could illustrate this better?


  5. Posts
    2,857
    Quote Originally Posted by NinjaTaco View Post
    Is there a simple script example that I could see that could illustrate this better?
    It would most likely look something like this:

    Code:  
    1. transform.rotation = Quaternion.FromToRotation(transform.up, normal) * transform.rotation;

    Can't guarantee I got that exactly right, but it should be something like that.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •