Building Placement Bug

Hi everyone,

I’m encountering the following problem.
I have a button that instantiates a prefab to the current mouse position and can rotate with the mouse scroll wheel. But when I press the button to instantiate it, the prefab does something weird.
I have recorded a video about this problem and I hope there is someone here that can explain me why that I have this problem and how to solve it?

The link to the video:
https://drive.google.com/open?id=1elWyqspLpzizAx6eDlALUNtKww0UuyPi

The code that I use for placing objects:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuildPrefab : MonoBehaviour
{
    //GAMEOBJECTS
    public GameObject placeableObjectPrefab;
    public GameObject currentPlaceableObject;

    //FLOATS
    private float mouseWheelRotation;
    public float RotationAmount = 45f;

    //BOOLEANS
    public bool isClicked = false;

    public void Select()
    {
        isClicked = true;
    }

    private void Update()
    {
        InstantiatePrefab();

        if (currentPlaceableObject != null)
        {
            MoveCurrentObjectToMouse();
            RotateFromMouseWheel();
            ReleaseIfClicked();
        }
    }

    private void InstantiatePrefab()
    {
        if (isClicked)
        {
            if (Input.GetMouseButtonDown(1))
            {
                currentPlaceableObject = null;
                isClicked = false;
            }
            else if (!Input.GetMouseButtonDown(1))
            {
                currentPlaceableObject = Instantiate(placeableObjectPrefab);
            }
        }
    }

    private void MoveCurrentObjectToMouse()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            currentPlaceableObject.transform.position = hit.point;
            currentPlaceableObject.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
        }
    }

    private void RotateFromMouseWheel()
    {
        Debug.Log(Input.mouseScrollDelta);
        mouseWheelRotation += Input.mouseScrollDelta.y;
        currentPlaceableObject.transform.Rotate(Vector3.up, mouseWheelRotation * RotationAmount);
    }

    private void ReleaseIfClicked()
    {
        if (Input.GetMouseButtonDown(0))
            currentPlaceableObject = null;

        isClicked = false;
    }
}

Your issue is that the raycast is hitting the object you just tried to place.

So how do I fix this? I’m still learning.

I did not download and watch the videos (it is probably better to upload them on a well-known portal), but assuming @lordconstant got it on point, then you have to ignore the object you want to place.

When there’s no more logic involved, you can choose either of those:

  1. Remove/disable the colliders of the object you want to place (or the preview object) and ensure you add/enable them afterwards.
  2. Assign a specific layer and make sure that layer is ignored by your raycast (there are overloads of the raycasting functions that take layer masks). There is already a pre-defined layer for that: the IgnoreRaycast layer. When you use that one, you won’t need to change the raycast at all, because that layer is already ignored by that API.

Thanks for answering, after somewhile I have finally found the solution to this!
I’ve wrote this line of code “currentPlaceableObject.layer = 11;” in the wrong place.
The wright place is that works for me:

private void ReleaseIfClicked()
    {
        if (Input.GetMouseButtonDown(0))
        {
            currentPlaceableObject.layer = 11;
            currentPlaceableObject = null;
        }

        isClicked = false;
    }