Search Unity

Making a building script, Issue with hit.normal/transform.localPosition

Discussion in 'Scripting' started by trudeaudm, Sep 27, 2014.

  1. trudeaudm

    trudeaudm

    Joined:
    Apr 17, 2013
    Posts:
    116
    I am setting up a building system where a player can place foundations on the ground using raycasts to get the position to place, the player can also build foundations against previously placed foundations and they lock together, with the same rotation and offset by the width/depth of the piece.

    It works fine so long as the foundations are not rotated.



    Here is the code I have.
    Code (CSharp):
    1.     RaycastHit hit;
    2.                 Vector3 fwd = transform.TransformDirection(Vector3.forward);
    3.                 if (Physics.Raycast(transform.position, fwd, out hit, buildDist, layerMask)){
    4.                     //Orient part to be placed based on raycast hit if hit is against buildable layer
    5.                     if (hit.transform.gameObject.layer == 14){
    6.                      
    7.                         // If the foundation is directed to be placed against an already placed foundation piece.
    8.                         if (hit.transform.gameObject.tag == "F"){
    9.                             selectedPart.transform.parent = hit.transform;
    10.                             selectedPart.transform.rotation = hit.transform.rotation;
    11.  
    12.                             selectedPart.transform.localPosition = new Vector3((hit.normal.x * 7),
    13.                                                                                 (0.0f),
    14.                                                                                 (hit.normal.z * 7));
    15.  
    16.  
    17.                         }
    18.  
    19.                         //If the Foundation is directed at anything other than another foundation.
    20.                         else {
    21.                             selectedPart.transform.position = hit.point;
    22.  
    23.                             selectedPart.transform.eulerAngles = new Vector3((rotAngleX),
    24.                                                                             (rotAngleY),
    25.                                                                             (rotAngleZ));
    26.                          
    27.                          
    28.                             //Rotate object by degree on keypress
    29.                             if(Input.GetKeyDown("r")){
    30.                                 rotAngleX = rotAngleX + 15;
    31.                             }
    32.                             if(Input.GetKeyDown("f")){
    33.                                 rotAngleX = rotAngleX + -15;
    34.                             }
    35.                             if(Input.GetKeyDown("q")){
    36.                                 rotAngleY = rotAngleY + 15;
    37.                             }
    38.                             if(Input.GetKeyDown("e")){
    39.                                 rotAngleY = rotAngleY + -15;
    40.                             }
    41.                             if(Input.GetKeyDown("z")){
    42.                                 rotAngleZ = rotAngleZ + 15;
    43.                             }
    44.                             if(Input.GetKeyDown("c")){
    45.                                 rotAngleZ = rotAngleZ + -15;
    46.                             }
    47.                         }
    48.  
    49.                     }
     
  2. trudeaudm

    trudeaudm

    Joined:
    Apr 17, 2013
    Posts:
    116
    No one has any help to offer on this?
     
  3. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Try using localRotation on the second platform, then after rotating it, parent it.
     
  4. trudeaudm

    trudeaudm

    Joined:
    Apr 17, 2013
    Posts:
    116
    Trying that actually makes it worse, as currently the offset is all that is off, my rotation is perfectly aligned.

    It is almost like my offset values are getting skewed somehow.
     
  5. trudeaudm

    trudeaudm

    Joined:
    Apr 17, 2013
    Posts:
    116
    ZO5KmUG6R likes this.