Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Placing Object Problem!

Discussion in 'Scripting' started by Wister1230, Jul 10, 2015.

  1. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    Hello all,
    I tried to make a house crafting system but I got a problem.
    When the rotation in "Y" pos = 0 it works, if the rotation is different then 0 its not working!
    Here when it works: http://prntscr.com/7r03ul
    Here when it's not working: http://prntscr.com/7r05q3
    When the rotation is different then 0 it's not working!
    Here the code:
    Code (CSharp):
    1.             Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0.0f));
    2.             RaycastHit hit;
    3.  
    4.             if (Physics.Raycast(ray, out hit, 500.0f,HouseItem))
    5.             {
    6.                 Vector3 MyNormal = hit.normal;//hits the first point normal that is colliding.
    7.  
    8.                 Debug.Log(hit.transform.gameObject.name);
    9.                
    10.                 MyNormal = hit.transform.TransformDirection(MyNormal);//Transforms direction x, y, z from local space to world space.
    11.                
    12.                 if(MyNormal == hit.transform.forward)// if the direction == forward - spawning a block in pos "Z" + 1.0f, Istantiate = spawning something.
    13.                 {
    14.                     PlacingObject.transform.position = new Vector3(hit.transform.position.x, hit.transform.position.y, hit.transform.position.z + 10.0f);
    15.                     PlacingObject.transform.eulerAngles = new Vector3(hit.transform.eulerAngles.x, hit.transform.eulerAngles.y, hit.transform.eulerAngles.z);
    16.                 }
    17.                 else if(MyNormal == -hit.transform.forward)
    18.                 {
    19.                     PlacingObject.transform.position = new Vector3(hit.transform.position.x, hit.transform.position.y, hit.transform.position.z - 10.0f);
    20.                     PlacingObject.transform.eulerAngles = new Vector3(hit.transform.eulerAngles.x, hit.transform.eulerAngles.y, hit.transform.eulerAngles.z);
    21.                 }
    22.                 /*else if(MyNormal == hit.transform.up)
    23.                 {
    24.                     PlacingObject.transform.position = new Vector3(hit.transform.position.x, hit.transform.position.y + 10.0f, hit.transform.position.z);
    25.                     PlacingObject.transform.eulerAngles = new Vector3(hit.transform.eulerAngles.x, hit.transform.eulerAngles.y, hit.transform.eulerAngles.z);
    26.                 }
    27.                 else if(MyNormal == -hit.transform.up)
    28.                 {
    29.                     PlacingObject.transform.position = new Vector3(hit.transform.position.x, hit.transform.position.y - 10.0f, hit.transform.position.z);
    30.                     PlacingObject.transform.eulerAngles = new Vector3(hit.transform.eulerAngles.x, hit.transform.eulerAngles.y, hit.transform.eulerAngles.z);
    31.                 }*/
    32.                 else if(MyNormal == hit.transform.right)
    33.                 {
    34.                     PlacingObject.transform.position = new Vector3(hit.transform.position.x + 10.0f, hit.transform.position.y, hit.transform.position.z);
    35.                     PlacingObject.transform.eulerAngles = new Vector3(hit.transform.eulerAngles.x, hit.transform.eulerAngles.y, hit.transform.eulerAngles.z);
    36.                 }
    37.                 else if(MyNormal == -hit.transform.right)
    38.                 {
    39.                     PlacingObject.transform.position = new Vector3(hit.transform.position.x - 10.0f, hit.transform.position.y, hit.transform.position.z);
    40.                     PlacingObject.transform.eulerAngles = new Vector3(hit.transform.eulerAngles.x, hit.transform.eulerAngles.y, hit.transform.eulerAngles.z);
    41.                 }
    42.             }
    Thanks for help!
     
  2. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    I believe the origin of the model is not at the correct location in the center of the model. Therefore when you are rotating object it is not rotating at the center so it is causing the expected positions to look incorrect.
     
  3. Wister1230

    Wister1230

    Joined:
    Jun 6, 2015
    Posts:
    50
    How Can I fix it?